MVC3 Request.Form值为null

时间:2015-03-15 07:08:49

标签: c# asp.net-mvc-3 null request.form

问题:在控制器中,Request.Form值为null,count为0.这是在使用Web托管公司托管我的网站后发生的。但是在我开发网站时没有问题。

HTML PART

<form action = "@ViewBag.SearchLink" method="post" style="vertical-align:middle" name="ListSearch" enctype="application/x-www-form-urlencoded">
<table style="width:100%;vertical-align:middle;background-color:#265A9A;border-color:Black;color:White" >
    <tr align="right"><td>
    <input id="SearchWords" name="SearchWords" type="text" style="font-size:small" maxlength="500"/>
    <select id="SearchParam" name="SearchParam" style="font-size:small">
    @for (int i = 0; i < ViewBag.ParamCount; i++)
    {
        if (@ViewBag.SelectedParam == @ViewBag.ParamArray[i].ParamName)
        {
            <option selected='selected' >@ViewBag.ParamArray[i].ParamName</option>
        }
        else
        {
            <option>@ViewBag.ParamArray[i].ParamName</option>            
        }
    }
    </select>
    <input class="button_grad" type="submit" value=" Search Param " style="font-size:small"/>
    </td></tr>
</table>

控制器部分

public ActionResult SearchList()
{   
    try
    {
        string searchText = string.Empty;
        if (Request.Form["SearchWords"] != null)
        {
            searchText = Request.Form["SearchWords"].ToString().TrimEnd();
            // Does not enter this if condition  
        }

        string searchParam = Request.Form["SearchParam"].ToString();
        int count = Request.Form.Count; // returns 0

我查看了其他链接,但他们没有帮助。我错过了什么吗?请指教。

添加另一个页面的完整HTML,该页面也将Request.Form值返回为null。

HTML PART

@*
Author and Owner:   
Modified date:      
File Name:          Index
File Type:          CSHTML   
File Title:         Feedback
Description: 
*@

@{
    ViewBag.Title = "Feedback";
}

<h2>Feedback</h2>

<form name="AddFeedback" id="AddFeedback" action="@ViewBag.AddFeedbackLink" method="post" onsubmit="return Validation()" enctype="application/x-www-form-urlencoded" >
<table style="width:35%">
<tr><td><label style="width:35%">Name *</label></td><td style="width:65%"><input id="Name" name="Name" maxlength="200" style="font-size:small" value="@ViewBag.FeedbackName" onchange ="ControlLeave(this)"/> <br /></td></tr>
<tr><td><label style="width:35%">Email *</label></td><td style="width:65%"><input id="EmailId" name="EmailId" maxlength="100" value="@ViewBag.EmailId" style="font-size:small"/> <br /></td></tr>
<tr><td><label style="width:35%">Contact Number</label></td><td style="width:65%"><input id="ContactNumber" name="ContactNumber" maxlength="20" style="font-size:small"/> <br /></td></tr>
<tr><td><label style="width:35%">Type *&nbsp</label></td><td style="width:65%"><select id="Type" name = "Type"><option>Advertising</option><option>Comment</option><option>Grievance</option><option>Suggestion</option></select></td></tr>
<tr><td><label>Feedback Box *</label><br /></td></tr>
</table>
<br />
<textarea rows="10" cols="60"  name="Feedback" id="Feedback" style="font-size:small;background-color:#F2F2F2;"></textarea> <br /><br />
<label id="Message" style="font-size:small;color:Red">@ViewBag.FeedbackMessage</label><br /><br />
<input class="button_grad" type="submit" value=" Send "/>

</form>

请告知为什么上面的代码可以将Request.Form返回为null。 另请注意,该网站正在使用 Microsoft Visual Web Developer 2010 Express。

1 个答案:

答案 0 :(得分:0)

通过以下内容更改您的控制器实现。

public ActionResult SearchList(FormCollection formcollection)
{   
    try
    {
        string searchText = string.Empty;
        if (formcollection["SearchWords"] != null)
        {
            searchText = formcollection["SearchWords"].ToString().TrimEnd();
            // Does not enter this if condition  
        }

        string searchParam = formcollection["SearchParam"].ToString();

将Request.From更改为formcollection [&#34;&#34;]