可以';t向MVC FormCollection添加控件

本文关键字:MVC FormCollection 添加 控件 可以 | 更新日期: 2023-09-27 18:21:52

这必须是基本的,但我想在FormCollection中添加一个HTML控件,您可以在控件的name属性上设置一个值。不过,这似乎没有任何影响。已经在下面的代码中用两个控件尝试过了——txtRating和FileUpload(都接近底部)。我做错了什么?该方法被正确调用,并将Html辅助控件放在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>
}

编辑:

方法如下:

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
  • 餐厅ID
  • IsNewReview
  • 用户ID
  • 注释
  • 餐厅名称加附近
  • VisitDate
  • MealTypeName
  • 注释
  • IsDiscountPrice
  • GoodValue
  • BadValue

谢谢,Rob

可以';t向MVC FormCollection添加控件

  1. txtRating-是一个标签。将intput(类型为"hidden"或"text")与value属性一起使用
  2. uploadFile-使用类型"file"输入。检查操作方法中的Request.Files集合