在渲染ActionLink时如何模拟模型绑定行为?

时间:2011-07-30 19:37:35

标签: asp.net-mvc asp.net-mvc-3 model-binding

在下面的代码中,get动作返回给定比赛日期的投注卡,帖子我使用 post 动作将绑定模型的属性转换为获取行动。

详情视图的基本方面:

@using (Html.BeginForm("Upload", "BettingCard",
                         FormMethod.Post, new { id = "uploadForm", enctype = "multipart/form-data" }))
{ 
    @Html.ValidationSummary(true, "The upload was unsuccessful.  The following error(s) occurred: ")
    <div id="date-selector">
        <div id="ymd">
            @Html.LabelFor(model => model.RaceDate)
            @Html.DropDownListFor(model => model.RaceDay, Model.YmdLists.Days)
            &nbsp; @Html.DropDownListFor(model => model.RaceMonth, Model.YmdLists.Months)
            &nbsp; @Html.DropDownListFor(model => model.RaceYear, Model.YmdLists.Years)
            &nbsp;&nbsp;<input type="submit" value="Upload for this date" />
        </div>
    </div>
    @Html.Telerik().Upload().Name("UploadedFiles")
}

控制器代码的基本方面:

[HttpGet]
public ActionResult Details(int year, int month, int day) {
    var model = new BettingCardModel
                    {
                        ResultMessage = "No betting card was located for the selected date."
                    };
    DateTime passedDate;
    if (!DateTimeHelper.TrySetDmy(year, month, day, out passedDate)) {         
        ModelState.AddModelError("", "One or more values do not represent a valid date.");
        return View(model);
    }
    model.RaceDate = passedDate;
    var bettingCard = _bettingCardService.GetByRaceDate(passedDate);
    model.MapFromEntity(bettingCard);
    return View(model);
}

[HttpPost]
public ActionResult Details(BettingCardModel model)
{
    return RedirectToAction("Details", new { year = model.RaceYear, month = model.RaceMonth, day = model.RaceDay });
}

上面的大量代码都是实验性的和诊断性的,所以我想避免对有用的代码进行审查,而是专注于我需要实现的目标。在Details视图中,我只需要一个'命令',即'显示日期',因此我可以通过使用提交按钮轻松下载,而http文章则负责模型绑定。但是,在Upload视图中,我需要两个命令,即“显示日期”和“上传日期”,因此我希望“显示日期”严格按照获取< / strong>操作,并且仅使用帖子操作提交该日期的上传投注卡。

我的问题是,当我使用“显示日期”命令时,使用ActionLink而不是提交,使用model.RaceDay等作为路由值,传递给Details的网址参数仍然包含它们的初始值,而不是用户在下拉列表中设置的值。似乎没有为动作链接调用模型绑定代码(无论可能是什么)。我可以在这做什么,以避免只需要一个帖子来做绑定?

我意识到这可能不是直接的模型绑定问题,但我不知道如何表达我的问题。当渲染元素“绑定”到模型属性时,它们比简单输入(例如,一些基本样式)更侧面,但是某些东西是围绕具有大量元数据的输入“构建”的。我希望通过某种方式在点击页面上的获取链接时使用该元数据映射到网址。

1 个答案:

答案 0 :(得分:1)

您遇到的问题是所有模型数据和元数据都是在服务器上动态生成的,并作为静态内容提供给客户端。只有在将模型提交到服务器后,绑定才会知道模型的更改。所有模型元数据在客户端都是静态的,使用纯.NET时,它无法知道用户何时更改下拉列表中的值以更改静态锚标记中的值,这就是ActionLink的内容。呈现给。答案是使用javascript。有很多方法可以通过javascript完成你想要做的事情。你可以写一个自定义的HtmlHelper类来为你生成javascript。但是,如果您不想使用javascript,那么您将需要发布帖子以将用户选择的数据提供给服务器。

如果您尝试避免重新编写代码,则可以为表单内容创建部分视图,并将其嵌入到两个单独的视图中。您可以尝试的另一件事是通过使用两个具有相同名称的提交按钮来检测按下了哪个按钮:

    <input type="submit" name="command" value="Update" />
    <input type="submit" name="command" value="Display" />

然后在[HttpPost]操作的控制器中,您可以检测到通过Request.Forms推送的内容如下:

[HttpPost]
public ActionResult Details(BettingCardModel model)
{
    if (Request.Forms["command"].Equals("Display"))
    {
        return RedirectToAction("Details", new { year = model.RaceYear, month = model.RaceMonth, day = model.RaceDay });
    }

    // Do your update code here...
    return // Whatever it is you return for update.
}

希望这会对你有所帮助。