与搜索发布到局部视图ASP.NET索引页 [英] ASP.NET index page with search posting into partial view

查看:128
本文介绍了与搜索发布到局部视图ASP.NET索引页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索框(直接从本教程索引页:<一href=\"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\" rel=\"nofollow\">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创建HTTP POST功能被称为

我如何让搜索表单停止发布也进入我的局部视图?

我index.cshml:

  @using(Html.BeginForm(「指数」))
    {
    &所述p为H.;
        @ Html.TextBox(SearchString在ViewBag.CurrentFilter字符串,新{@class =搜索查询,占位符=按名称搜索})
        &LT;输入类型=提交值=搜索级=BTN/&GT;
    &所述; / P&GT;
    }
    @ Html.Action(创建);

我Create.cshtml:

  @using(Html.BeginForm(创建)){
    @ Html.TextBoxFor(型号=&GT; model.Title,新{@style =WIDTH:250px的})
    @ Html.TextBoxFor(型号=&GT; model.AnnouncementText,新{@style =WIDTH:250px的})
    &LT;输入类型=提交值=创建类=BTN BTN-小/&GT;
    @ Html.ValidationMessageFor(型号=&GT; model.Title)
    @ Html.ValidationMessageFor(型号=&GT; model.AnnouncementDate)
}

我的控制器:

 公开的ViewResult指数(字符串搜索字符串,诠释?页)
    {
        在db.Announcements VAR公告=从
                            选择一个;
        如果(!String.IsNullOrEmpty(搜索字符串))
        {
            ViewBag.Search = TRUE;
            公告= Announcements.Where(S =方式&gt;(s.Title.ToUpper()包含(searchString.ToUpper())|| s.AnnouncementText.ToUpper()包含(searchString.ToUpper())));
        }
        公告= Announcements.OrderBy(S =&GT; s.Title);        INT的pageSize = 10;
        INT PAGENUMBER =(第53页1);
        返回查看(Announcements.ToPagedList(PAGENUMBER,pageSize的));
    }    公众的ActionResult的Create()
    {
        公告公告=新公告();
        返回PartialView(公告);    }    //
    // POST:/公告/创建    [HttpPost]
    公众的ActionResult创建(公告公告)
    {
        如果(ModelState.IsValid)
        {
            db.Announcements.Add(公告);
            db.SaveChanges();
            返回RedirectToAction(「指数」);
        }        返回查看(公告);
    }


解决方案

我还是不完全了解是什么原因导致这个问题,但我通过重命名创建邮政法CreatePost固定它。当GET和POST方法都具有相同的名称,由于某种原因,POST已被调用。

我create.cshtml

  @using(Html.BeginForm(CreatePost))

感谢所有您的帮助。

I have an Index page with a search box (straight from this tutorial: 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) to which I've added a Create form in the footer, which I display as a partial view.

Although the Create form works well and adds new records, the search form seems to post back the Create Form instead, causing a validation error, and redisplaying the whole layout.cshtml page within the location of my partial view.

Edit - the search form posts itself and returns the correct result and then seems to post my partial view as well. My debugger shows the controllers ActionResult Create HTTP post function being called

How do I get the Search form to stop also posting into my partial view?

My 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"); 

My 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)
}

My controller:

    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);
    }

解决方案

I still don't entirely understand what caused the problem, but I fixed it by renaming the Create Post method to CreatePost. When the GET and POST methods both had the same name, for some reason the POST was being called.

My create.cshtml

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

Thanks all for your help

这篇关于与搜索发布到局部视图ASP.NET索引页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆