0

我有一个带有搜索框的索引页面(直接来自本教程: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);
    }
4

2 回答 2

0

我仍然不完全了解导致问题的原因,但我通过将 Create Post 方法重命名为 CreatePost 来修复它。当 GET 和 POST 方法都具有相同的名称时,由于某种原因,正在调用 POST。

我的 create.cshtml

@using (Html.BeginForm("CreatePost"))

感谢你的帮助

于 2013-07-02T01:41:32.237 回答
0

一种可能的解决方案是使用 ajax 将部分内容发布回视图,而无需重新加载整个页面。如果您使用 Ajax.BeginForm{},那么您的控制器可以将您的部分作为 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);
        }
于 2013-07-01T01:24:50.807 回答