1

使用 JQuery 可排序,并尝试将新订单发送回我的控制器,但运气不佳。我的看法是:

using (Ajax.BeginForm("EditTickerOrder", new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "POST", }))
{
    <div id="editableticker">
        @Html.HiddenFor(m => m.ProjectGUID)
        <ul id="sortablediv">
            @foreach (DGI.CoBRA.Tools.BussinessObjects.CollabLibrary.TickerObjects.Ticker t in Model)
            {
                <li class="ui-state-default" id="@t.pKeyGuid.ToString()">
                    <p>@Html.CheckBox(t.pKeyGuid.ToString(), t.Display, new { @class = "activechk" }) 
                        <span style="font-weight: bold">
                            @t.Text
                        </span>
                    </p>
                </li>
            }
        </ul>
    <input type="submit" value="Save New Ticker Order" />
}

我的控制器是:

[HttpPost]
public ActionResult EditTickerOrder(Guid ProjectGUID, List<string> items)
{
    TickerCollectionModel TickerData = new TickerCollectionModel();
    TickerData.ProjectGUID = ProjectGUID;
    TickerData.ListAllBySession(ProjectGUID);
    return PartialView("TickerList", TickerData);
}

然而list<string> items总是如此null。有任何想法吗?

4

1 回答 1

2

您正在编写foreach循环,这肯定违反了默认模型绑定器期望用于处理集合的表单输入字段的命名约定。如果您不尊重已建立的连线格式,则不能期望默认模型绑定器能够在 POST 操作中重新水合您的模型。

事实上,你为什么不使用视图模型和编辑器模板呢?它们使 ASP.NET MVC 中的一切变得微不足道。

因此,让我们定义一个反映您的视图要求的视图模型(或者至少在您的问题中显示的那些=>您当然可以使用您想要处理的其他属性来丰富它):

public class TickerViewModel
{
    public Guid Id { get; set; }
    public bool IsDisplay { get; set; }
    public string Text { get; set; }
}

public class ProjectViewModel
{
    public Guid ProjectGUID { get; set; }
    public IEnumerable<TickerViewModel> Tickers { get; set; }
}

然后是一个控制器,其职责是查询您的 DAL 层,检索域模型,将域模型映射到我们为此视图定义的视图模型中,并将视图模型传递给视图。相反,POST 操作从视图接收视图模型,将视图模型映射回某个域模型,将域模型传递给 DAL 层进行处理并呈现某些视图或重定向到成功操作:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        // TODO: those values come from a data layer of course
        var model = new ProjectViewModel
        {
            ProjectGUID = Guid.NewGuid(),
            Tickers = new[]
            {
                new TickerViewModel { Id = Guid.NewGuid(), Text = "ticker 1" },
                new TickerViewModel { Id = Guid.NewGuid(), Text = "ticker 2" },
                new TickerViewModel { Id = Guid.NewGuid(), Text = "ticker 3" },
            }
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(ProjectViewModel model)
    {
        // Everything will be correctly bound here => map the view model
        // back into your domain model and pass the domain model to 
        // your DAL layer for processing ...

        return Content("Thanks for submitting");
    }
}

一个视图(值得注意的是,在此示例中,我使用了标准表单而不是 AJAX,但将其转换为 AJAX 表单很简单):

@model ProjectViewModel

@using (Html.BeginForm())
{
    @Html.HiddenFor(m => m.ProjectGUID)
    <div id="editableticker">
        <ul id="sortablediv">
            @Html.EditorFor(x => x.Tickers)
        </ul>
    </div>
    <button type="submit">OK</button>
}

最后是相应的编辑器模板,它将为 Tickers 集合的每个元素自动呈现(~/Views/Home/EditorTemplates/TickerViewModel.cshtml):

@model TickerViewModel

<li class="ui-state-default">
    <p>
        @Html.CheckBoxFor(x => x.IsDisplay, new { @class = "activechk" }) 
        @Html.LabelFor(x => x.IsDisplay, Model.Text)
        @Html.HiddenFor(x => x.Text)
        @Html.HiddenFor(x => x.Id)
    </p>
</li>
于 2012-06-10T19:35:06.397 回答