MVC控制器在提交后未启动
本文关键字:启动 提交 控制器 MVC | 更新日期: 2023-09-27 18:25:44
因此,在从视图提交表单后,我很难在控制器中激发POST操作。以下是我迄今为止的一些代码:
模型-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; }
}
控制器-SearchController.cs
[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>
}
当点击提交按钮时,视图会将模型传递给控制器操作"搜索",但我会返回一个白色屏幕。
正如你在代码中看到的,我在一个名为"Area1"的区域,但这应该没有多大区别。我希望像描述的那样工作,而不是使用Ajax调用。
我已经考虑过了,但我没有取得多大成功,到目前为止我所做的似乎是正确的,但不幸的是,事实并非如此。
任何让控制器在提交按钮上启动的帮助都将是惊人的。
编辑:当我说fire时,我在Search方法中设置了一个断点,但该断点没有被命中。
编辑2:这是创建新区域时生成的Area1AreaRegistration.cs文件中的地图路线:
public class Area1AreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Area1";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
// tried with and without this
//context.MapRoute(
// name: "Search",
// url: "Area1/Search",
// defaults: new { controller = "Search", action = "Search", id = UrlParameter.Optional }
// );
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
模型位于~/Areas/Area1/Models也是毫无价值的,控制器位于~/Areas/Area1/Controllers,视图位于~/Aares/Area1/Views
问题很简单。如果提交到Search视图,并在同一个Search视图中返回结果,MVC会将其解释为无效的模型状态。这就是什么都没发生的原因。即使你放:
if (ModelState.IsValid) {
return View("Search", searchModel);
}
您会注意到ModelState
是有效的,但不会发生任何事情。为了解决问题,创建接收Post的SearchResult
操作或执行RedirectToAction("Search", searchModel)
这应该能解决问题。
对不起,我是stackerflow的新成员,没有足够的分数可以留下评论,所以我试着在这里回答你的问题。也许你可以尝试一下,看看它是否执行该方法:
public ActionResult Search(FormCollection form)
您可以使用访问传入的内容
string a = form["yourTextboxID"].ToString();