在提交时用MVC填充表

本文关键字:填充 MVC 提交 | 更新日期: 2023-09-27 17:50:30

我正在学习MVC转换旧的Web表单到MVC 5。这个页面非常简单,只有几个文本框、一个提交按钮和一个网格。我想要类似的功能,在页面加载时没有网格,提交发生后,网格出现。我遇到的问题是我所看到的所有示例都包含页面加载时的数据。

这是我的视图

<h2>Search Craigslist</h2>
@using (Html.BeginForm())
{
    <p>
        Enter Search Criteria:<br />
        @Html.TextBox("Criteria", null, new { @style = "width: 450" })
        @Html.DropDownList("DropDownValues")
        Days:
        @Html.TextBox("Days")
        <br />
        <input type="submit" value="Submit" />
    </p>
}
@Html.Partial("~/Views/Search/_Search.cshtml")

和部分

@model IEnumerable<CraigsListSearch.MVC.Models.SearchModel>
@{
Layout = null;
}

<table cellspacing="0" border="1" style="border-collapse:collapse;">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Location)
    </th>
    <th>
        Link
    </th>
    <th>
        @Html.DisplayNameFor(model => model.DateSubmitted)
    </th>
</tr>
@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Location)
        </td>
        <td>
            @Html.Hyperlink(Html.DisplayFor(modelItem => item.URL).ToString(), Html.DisplayFor(modelItem => item.Title).ToString())
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DateSubmitted)
        </td>
    </tr>
}

最后是控制器

public class SearchController : Controller
{
    [HttpGet]
    //public ViewResult Index(string DropDownValues, string Criteria, string Days)
    public ViewResult Index()
    {
        Populate();
        Session["list"] = SearchModel.GetQuickList();
        return View(new List<SearchModel>());
    }
    [HttpPost]
    public ViewResult Index(string DropDownValues, string Criteria, string Days)
    {
        Populate();
        Criteria = Server.UrlPathEncode(Criteria).Replace(",", "%2c");
        List<Location> list = (List<Location>)(Session["list"]);
        List<SearchModel> results = SearchModel.Search(Criteria, Days, DropDownValues, list.Take(5).ToList());
        return View(results);
    }
    private void Populate()
    {
        List<DropDownModel> ddlList = DropDownModel.GetDropdowns();
        ViewBag.DropDownValues = (from a in ddlList
                                  select new SelectListItem
                                  {
                                      Text = a.Text,
                                      Value = a.Value
                                  }).ToList();
    }
}

我要找的是在POST上只"调用"部分。这是一件容易做到的事情吗?如果还需要其他信息,请告诉我。

谢谢!

在提交时用MVC填充表

有几种方法可以解决这个问题。

一种选择是使用AJAX。当用户单击按钮时,对控制器进行AJAX调用,并让控制器呈现部分内容并返回HTML,然后将HTML注入页面DOM。

另一个选择是在你的模型中添加一个"DisplayGrid"属性。当你第一次渲染视图时,将其设置为false,然后在post后将其设置为true。

我想说的一点是,从Post动作渲染View并不是一个好主意。如果用户在浏览器中点击刷新,它会再次发布,给用户一个烦人的丑陋对话框,告诉他们要向服务器发送数据。一般来说,您应该使用Post-Redirect-Get模式,用户发布,您重定向,然后您使用get在浏览器中重新呈现。如果使用AJAX方法,这一切都将变得毫无意义,因为您永远不会将整个表单发送回控制器操作。如果你继续使用完整的post, HttpPost版本的Index应该返回一个RedirectToAction结果,将用户发送回Index的Get版本。您可以使用TempData在这两个操作之间进行通信,例如:

[HttpGet]
public ActionResult Index()
{
    if( TempData.ContainsKey( "DisplayGrid" )
    {
        // Use the other values from TempData to populate the model with the grid data
        myModel.DisplayGrid = (bool)TempData["DisplayGrid"];
    }
}
[HttpPost]
public ActionResult Index( string dropDownValues, string criteria, string days )
{
    TempData["DisplayGrid"] = true;
    TempData["DropDownValues"] = dropDownValues;
    TempData["Criteria"] = criteria;
    TempData["Days"] = days;
    return RedirectToAction( "Index" );
}

你应该传递任何你需要通过模型(或ViewBag)渲染视图的信息。

既然您已经有了如何使用Model的示例,那么您可以使用ViewBag

@if(ViewBag.RenderSearch)
{
  @Html.Partial("~/Views/Search/_Search.cshtml")
}

设置RenderSearch特殊动作:

public ViewResult Index(string DropDownValues...
{
     ....
     ViewBag.RenderSearch = true;

与模型只需添加属性到您的模型(您需要使其自定义类而不是仅仅List),并检查该标志,而不是

试试这个,Ajax只替换GridDiv只

<h2>Search Craigslist</h2>
using (Ajax.BeginForm("BindGridAction", "Contoller",new AjaxOptions { UpdateTargetId = "GridDiv" }))
{
    <p>
        Enter Search Criteria:<br />
        @Html.TextBox("Criteria", null, new { @style = "width: 450" })
        @Html.DropDownList("DropDownValues")
        Days:
        @Html.TextBox("Days")
        <br />
        <input type="submit" value="Submit" />
    </p>
    }
<div id="GridDiv">
    @Html.Partial("~/Views/Search/_Search.cshtml")
</div>

在控制器中绑定局部视图

    public PartialViewResult BindGridAction()
    {
       // your model coding
        return PartialView("~/Views/Search/_Search.cshtml", model);
    }