Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC控制器在提交后未触发_C#_Asp.net Mvc_Asp.net Mvc 4_Razor - Fatal编程技术网

C# MVC控制器在提交后未触发

C# MVC控制器在提交后未触发,c#,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,因此,在从视图提交表单后,我很难让控制器中的POST操作启动。以下是我目前掌握的一些代码: Model-SearchModel.cs public class SearchModel { public string FormName { get; set; } public string Category { get; set; } public string EventType { get; set; } public string UserId { get;

因此,在从视图提交表单后,我很难让控制器中的POST操作启动。以下是我目前掌握的一些代码:

Model-SearchModel.cs

public class SearchModel
{
    public string FormName { get; set; }
    public string Category { get; set; }
    public string EventType { get; set; }
    public string UserId { get; set; }
}
[HttpPost]
public ActionResult Search(SearchModel searchModel) // This is not Firing
{
    // other stuff
    return View("Search", searchModel);
}
Controller-SearchController.cs

public class SearchModel
{
    public string FormName { get; set; }
    public string Category { get; set; }
    public string EventType { get; set; }
    public string UserId { get; set; }
}
[HttpPost]
public ActionResult Search(SearchModel searchModel) // This is not Firing
{
    // other stuff
    return View("Search", searchModel);
}
查看-搜索.cshtml

@model MainProject.Areas.Area1.Models.SearchModel
@{
    ViewBag.Title = "Search";
}

@using (Html.BeginForm("Search", "Search", new { area = "Area1"}, FormMethod.Post, new { id = "fmSearch" }))
{
        <div id="searchDiv">
            <fieldset>
                <legend>Search</legend>
                    <div>
                        <table style="padding-left: 110px">                                               
                            <tr>
                                <td>
                                    <label for="FormName">Form Name</label>
                                </td>
                                <td>
                                    @Html.TextBoxFor(m => m.FormName, new { @id = "txtFormName", @class = "rounded formNumber uppercase", style = "width: 253px;", placeholder = "Form Name", maxlength = 12 })
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <label for="Category">Category</label>
                                </td>
                                <td>
                                    @Html.TextBoxFor(m => m.Category, new { @id = "txtCategory", @class = "rounded uppercase", style = "width: 253px;", placeholder = "Category", maxlength = 12 })
                                </td>
                                <td style="width: 100px"></td>
                                <td>
                                    <label for="EventType">Event Type</label>
                                </td>
                                <td>
                                    @Html.TextBoxFor(m => m.EventType, new { @id = "txtEventType", @class = "rounded uppercase", style = "width: 253px;", placeholder = "Event Type", maxlength = 12 })
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <label for="UserId">User ID</label>
                                </td>
                                <td>
                                    @Html.TextBoxFor(m => m.UserId, new { @id = "txtUserId", @class = "rounded uppercase", style = "width: 253px;", placeholder = "User ID", maxlength = 12 })
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <input class="roundedbutton" type="submit" id="btnSearch" value="Search" name="button" title="Search" style="width:120px; margin-right: 15px; margin-top: 10px; margin-bottom: 5px;" />
                                </td>
                            </tr>
                        </table>
                    </div>
        </fieldset>
    </div>
}
该模型位于~/Areas/Area1/Models,这一点也不值一提,
控制器位于~/Areas/Area1/Controllers,视图位于~/Areas/Area1/Views

问题非常简单。如果提交到搜索视图,并在同一个搜索视图中返回结果,MVC会将其解释为无效的模型状态。这就是什么也没发生的原因。即使你把:

if (ModelState.IsValid) {
    return View("Search", searchModel);
}
您会注意到,
ModelState
是有效的,但不会发生任何事情。要解决此问题,请创建接收帖子的
SearchResult
操作,或执行
重定向到操作(“搜索”,searchModel)


这应该可以解决问题。

很抱歉,我是stackoverflow的新成员,没有足够的分数留下评论,所以我尝试在这里回答您的问题。可能您可以尝试此操作,以查看它是否执行该方法:

public ActionResult Search(FormCollection form)
您可以使用访问传入的内容

string a = form["yourTextboxID"].ToString();

你是说如果你在方法中放置了一个断点,当你提交时它不会被命中吗?这是正确的。我在方法中设置了一个断点,但它没有被命中。您确定区域正确吗(尝试从表单标记中删除
new{area=“Area1”}
)是的。我也试过了。仍然得到相同的结果。是否修改了默认的MapRoute?问题不是我得到的是一个空白屏幕,而是当我在方法中设置断点时,搜索方法没有被命中。因此,我认为实际上没有任何内容返回到视图。视图和操作是否位于区域1\SearchController\Search?这是正确的。Iv使用路由信息更新了上述问题。请尝试在MapRoute方法的默认值中添加控制器。