无法向MVC FormCollection添加控件

时间:2014-09-15 14:51:06

标签: c# asp.net-mvc

这必须是基本的但我想在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
  • RestaurantID
  • IsNewReview
  • UserID
  • 注释
  • RestaurantNamePlusVicinity
  • VisitDate
  • MealTypeName
  • 注释
  • IsDiscountPrice
  • GoodValue
  • BADVALUE

谢谢, 罗布

1 个答案:

答案 0 :(得分:1)

  1. txtRating - 是一个标签。使用int属性(键入&#39;隐藏&#39;或&#39;文本&#39;)和value属性。
  2. uploadFile - 输入类型&#39; file&#39;。在您的操作方法中检查Request.Files集合。