我有一个带有搜索框的索引页面(直接来自本教程:http ://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with -the-entity-framework-in-an-asp-net-mvc-application),我在页脚中添加了一个 Create 表单,我将其显示为部分视图。
尽管 Create 表单运行良好并添加了新记录,但搜索表单似乎改为回发 Create Form,导致验证错误,并在我的局部视图位置重新显示整个 layout.cshtml 页面。
编辑- 搜索表单本身发布并返回正确的结果,然后似乎也发布了我的部分视图。我的调试器显示控制器 ActionResult Create HTTP post 函数被调用
如何让搜索表单也停止发布到我的部分视图中?
我的 index.cshml:
@using (Html.BeginForm("Index"))
{
<p>
@Html.TextBox("SearchString", ViewBag.CurrentFilter as string, new { @class = "search-query", placeholder = "Search by name" })
<input type="submit" value="Search" class="btn" />
</p>
}
@Html.Action("Create");
我的 Create.cshtml:
@using (Html.BeginForm("Create"))
{
@Html.TextBoxFor(model => model.Title, new { @style = "width:250px" })
@Html.TextBoxFor(model => model.AnnouncementText, new { @style = "width:250px" })
<input type="submit" value="Create" class="btn btn-small" />
@Html.ValidationMessageFor(model => model.Title)
@Html.ValidationMessageFor(model => model.AnnouncementDate)
}
我的控制器:
public ViewResult Index(string searchString, int? page)
{
var Announcements = from a in db.Announcements
select a;
if (!String.IsNullOrEmpty(searchString))
{
ViewBag.Search = true;
Announcements = Announcements.Where(s => (s.Title.ToUpper().Contains(searchString.ToUpper()) || s.AnnouncementText.ToUpper().Contains(searchString.ToUpper())));
}
Announcements = Announcements.OrderBy(s => s.Title);
int pageSize = 10;
int pageNumber = (page ?? 1);
return View(Announcements.ToPagedList(pageNumber, pageSize));
}
public ActionResult Create()
{
Announcement announcement = new Announcement();
return PartialView(announcement);
}
//
// POST: /Announcement/Create
[HttpPost]
public ActionResult Create(Announcement announcement)
{
if (ModelState.IsValid)
{
db.Announcements.Add(announcement);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(announcement);
}