2014-09-15 3 views
0

Это должно быть базовым, но я решил добавить элемент управления HTML в FormCollection, который вы установили для атрибута name элемента управления. Это, похоже, не имеет никакого влияния. Попробовали это с двумя элементами управления - txtRating и FileUpload в коде ниже (оба внизу). Что я делаю не так? Метод вызывается правильно и помещает элементы управления Html в FormCollection, но не те, которые я пытался добавить. Чего не хватает?Невозможно добавить элемент управления в MVC FormCollection

@using (Html.BeginForm("SaveReview", "Content", FormMethod.Post, new { enctype = "multipart/form- data", id = "photoform" })) 
{ 
<fieldset> 
    @Html.Hidden("ReviewID", Model.ReviewID, new { @id = "hidReviewID" }) 
    @Html.Hidden("RestaurantID", null, new { @id = "hidRestaurantID" }) 
    @Html.Hidden("IsNewReview", Model.IsNewReview) 
    @Html.HiddenFor(m => m.UserID, new { @id = "hidUserID" }) 
    @if (Model.IsInEditMode) 
    { 
     <div class="bodyc"> 
      <div id="reviewsbody3" class="sectionbody1all"> 
       <div class="reviewtxtrestaurant"> 
        @Html.TextBoxFor(m => m.RestaurantNamePlusVicinity, htmlAttributes: new { id = "txtSearch", placeholder = "Search restaurant name", @style = "width:99%; margin:5px auto;display:block;" }) 
       </div> 
       Visit Date: @Html.TextBoxFor(m => m.VisitDate, "{0:dd-MM-yyyy}", htmlAttributes: new { id = "txtVisitDate", @readonly = "readonly" }) 
       Meal Type: @Html.DropDownListFor(m => m.MealTypeName, new SelectList(Model.MealTypes, "Value", "Text", "Dinner")) 
      </div> 
     </div> 
    } 
    else 
    { 
     <div class="bodyc"> 
      <div id="reviewsbody3" class="sectionbody1all"> 
       <div class="reviewtxtrestaurant">@Html.DisplayFor(m => m.RestaurantNamePlusVicinity, new { @style = "width:98%; margin:0px auto;display:block;" })</div> 
       Visit Date: @Html.DisplayFor(m => m.VisitDate, "{0:dd-MM-yyyy}") 
       Meal Type: @Html.DisplayFor(m => m.MealTypeName) 
      </div> 
      <div class="bottom"></div> 
     </div> 
    } 

    <div class="bodyc"> 
     <div id="reviewsbody53" class="sectionbody1all"> 
      @Html.TextAreaFor(m => m.Comment, new { id = "txtComment", maxlength = 120, @style = "width:99%;height:90%;margin:5px auto;display:block;" }) 
     </div> 
    </div> 

    <div class="bodyc"> 
     <div id="reviewsbody544" class="sectionbody1all"> 
      <div class="slidergiant" style="float:left;width:50%;display:inline-block;position:relative;height:140px;"> 
       <div style="margin:0px auto;"> 
        <div id="sli" class="slider2" style="width:95%;margin:58px 10px 10px 10px;"></div> 
        <label id="txtRatingDescription" style="font-size:20px;margin:0px auto; width:100%;text-align:center;display:block;">average to good</label> 
       </div> 
      </div><div class="ratinggiant" style="float:right;width:50%;display:inline-block;position:relative;"> 
       <div style="margin:0px auto;"> 
        <img id="imgScore" src="~/Images/scorebackground180.png" style="width:130px;height:130px;position:absolute;top:4px;left:0px;z-index:0.9;" /> 
        <label id="txtRating" name="txtRating" style="position:absolute;top:39px;left:32px;font-size:63px;color:white;z-index:1;">5.5</label> 
       </div> 
       <div class="valuechecks" style="width:100%;text-align:center;display: block;"> 
        @Html.Label("Discounted:")   @Html.CheckBoxFor(m => m.IsDiscountPrice) <br /> 
        @Html.Label("Good Value:")   @Html.CheckBoxFor(m => m.GoodValue)<br /> 
        @Html.Label("Bad Value:")   @Html.CheckBoxFor(m => m.BadValue) <br /> 
       </div> 
      </div> 
      <div style="clear: both"></div> 
     </div> 
    </div> 

    <div class="bodyc"> 
     <div id="reviewsbody4" class="sectionbody1all"> 
      <input type="file" id="uploadFile" name="uploadFile" onchange="showimagepreview(this);" /> 
      <div style="width:100px;height:100px;"><img id="imagepreview" alt="image preview" /></div> 
     </div> 
    </div> 
</fieldset> 
} 

Edit:

Метод заключается в следующем:

public ActionResult SaveReview(FormCollection f) 
{ 
} 

и он отправил через обычную кнопку с событием мыши установить через JQuery:

  $('#btnSave').click(function() { 
       $('#photoform').submit(); 
      }); 

И следующая генерируется HTML:

<form action="/Content/SaveReview" enctype="multipart/form-data" id="photoform" method="post">  
<fieldset> 
     <input data-val="true" data-val-required="The ReviewID field is required." id="hidReviewID" name="ReviewID" type="hidden" value="e0ee6f58-a8f0-4ff8-b6fc-3fd2ed591567" /> 
     <input data-val="true" data-val-required="The RestaurantID field is required." id="hidRestaurantID" name="RestaurantID" type="hidden" value="00000000-0000-0000-0000-000000000000" /> 
     <input data-val="true" data-val-required="The IsNewReview field is required." id="IsNewReview" name="IsNewReview" type="hidden" value="True" /> 
     <input data-val="true" data-val-required="The UserID field is required." id="hidUserID" name="UserID" type="hidden" value="07a6e730-57ca-4d1e-b52b-988d85759501" /> 
      <div class="bodyc"> 
       <div id="reviewsbody3" class="sectionbody1all"> 
        <div class="reviewtxtrestaurant"> 
         <input id="txtSearch" name="RestaurantNamePlusVicinity" placeholder="Search restaurant name" style="width:99%; margin:5px auto;display:block;" type="text" value="" /> 
        </div> 
        Visit Date: <input data-val="true" data-val-date="The field Visit Date must be a date." data-val-required="The Visit Date field is required." id="txtVisitDate" name="VisitDate" readonly="readonly" type="text" value="15-09-2014" /> 
        Meal Type: <select id="MealTypeName" name="MealTypeName"><option value="Breakfast">Breakfast</option> 
<option selected="selected" value="Dinner">Dinner</option> 
<option value="Lunch">Lunch</option> 
</select> 
       </div> 
      </div> 

     <div class="bodyc"> 
      <div id="reviewsbody53" class="sectionbody1all"> 
       <textarea cols="20" id="txtComment" maxlength="120" name="Comment" rows="2" style="width:99%;height:90%;margin:5px auto;display:block;"> 
</textarea> 
      </div> 
     </div> 

     <div class="bodyc"> 
      <div id="reviewsbody544" class="sectionbody1all"> 
       <div class="slidergiant" style="float:left;width:50%;display:inline-block;position:relative;height:140px;"> 
        <div style="margin:0px auto;"> 
         <div id="sli" class="slider2" style="width:95%;margin:58px 10px 10px 10px;"></div> 
         <label id="txtRatingDescription" style="font-size:20px;margin:0px auto; width:100%;text-align:center;display:block;">average to good</label> 
        </div> 
       </div><div class="ratinggiant" style="float:right;width:50%;display:inline-block;position:relative;"> 
        <div style="margin:0px auto;"> 
         <img id="imgScore" src="/Images/scorebackground180.png" style="width:130px;height:130px;position:absolute;top:4px;left:0px;z-index:0.9;" /> 
         <label id="txtRating" name="txtRating" style="position:absolute;top:39px;left:32px;font-size:63px;color:white;z-index:1;">5.5</label> 
        </div> 
        <div class="valuechecks" style="width:100%;text-align:center;display: block;"> 
         <label for="Discounted:">Discounted:</label>   <input data-val="true" data-val-required="The IsDiscountPrice field is required." id="IsDiscountPrice" name="IsDiscountPrice" type="checkbox" value="true" /><input name="IsDiscountPrice" type="hidden" value="false" /> <br /> 
         <label for="Good_Value:">Good Value:</label>   <input data-val="true" data-val-required="The GoodValue field is required." id="GoodValue" name="GoodValue" type="checkbox" value="true" /><input name="GoodValue" type="hidden" value="false" /><br /> 
         <label for="Bad_Value:">Bad Value:</label>   <input data-val="true" data-val-required="The BadValue field is required." id="BadValue" name="BadValue" type="checkbox" value="true" /><input name="BadValue" type="hidden" value="false" /> <br /> 
        </div> 
       </div> 
       <div style="clear: both"></div> 
      </div> 
     </div> 

     <div class="bodyc"> 
      <div id="reviewsbody4" class="sectionbody1all"> 
       <input type="file" id="uploadFile" name="uploadFile" onchange="showimagepreview(this);" /> 
       <div style="width:100px;height:100px;"><img id="imagepreview" alt="image preview" /></div> 
      </div> 
     </div> 
    </fieldset> 
</form> 

Значения в FormCollection в настоящее время:

  • ReviewID
  • RestaurantID
  • IsNewReview
  • Идентификатор_пользователя
  • Комментарий
  • RestaurantNamePlusVicinity
  • VisitDate
  • MealTypeName
  • Комментарий
  • IsDiscountPrice
  • GoodValue
  • BADVALUE

Спасибо, Роб

+0

Что вы подразумеваете под 'не имеет никакого влияния'? – rhughes

+0

Он не добавляет его в FormCollection. Установка атрибута name не имеет распознаваемого эффекта. – user3775501

+0

Можете ли вы показать аргументы функции POST? – rhughes

ответ

1
  1. txtRating - это метка. Используйте intput (введите «hidden» или «text») со значением value.
  2. uploadFile - вводится с типом 'файл'. Проверка коллекции Request.Files в вашем методе действий.
Смежные вопросы