我有一个带有搜索框的索引页面(直接来自本教程: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),我在页脚中添加了一个创建表单,我将其显示为部分视图。
虽然“创建”表单运行良好并添加了新记录,但搜索表单似乎会回发“创建表单”,导致验证错误,并在部分视图的位置重新显示整个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);
}
答案 0 :(得分:0)
一种可能的解决方案是使用ajax将部分发布回视图而不进行完整页面重新加载。如果您使用Ajax.BeginForm {},那么您的控制器可以将您的partial作为html作为模型属性返回,然后您只需在Ajax.BeginForm的successFunction上加载内容。我在基本控制器类中使用下面的方法,因为我经常在视图中返回部分内容。
<强>模型... 强>
public class BaseEditModel
{
public string PostAction{get;set;}
public string PostController{get;set;}
public string PartialViewContent{get;set;}
}
<强>控制器... 强>
public ActionResult(int someID)
{
BaseEditModel model=new BaseEditModel();
model.PostController="SaveController";
model.PostAction="SaveEntity";
model.PartialViewContent=this.RenderPartialViewToString("Partials/Entity/EntityEdit", model);
}
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
查看... 强>
@using(Ajax.BeginForm(
Model.PostAction,
Model.PostController,
null,
new AjaxOptions { OnSuccess = "editPostSuccess", OnFailure = "editPostFailure" },
new { id = "editBase_frmEdit", name = "editBase_frmEdit" }))
{
}
<script type="text/javascript">
function editPostSuccess(ajaxContext) {
if (ajaxContext.PartialViewContent != null)
$('#partialDiv').html(ajaxContext.PartialViewContent);
}
答案 1 :(得分:0)
我仍然不完全理解导致问题的原因,但我通过将Create Post方法重命名为CreatePost来修复它。当GET和POST方法都具有相同的名称时,由于某种原因正在调用POST。
我的create.cshtml
@using (Html.BeginForm("CreatePost"))
感谢大家的帮助