编辑模型对象未按预期绑定到视图

发布于 2024-08-04 19:15:08 字数 1029 浏览 4 评论 0原文

我正在使用 ASP.NET MVC (1.0)、Spark View Engine (1.0) 和 SubSonic (3.0.0.3)。

我在使“编辑”视图正常工作时遇到一些问题。因此,在我的控制器中,我有以下显示编辑表单视图的控制器操作:

[AcceptVerbs(HttpVerbs.Get)]
public virtual ActionResult Edit(string name)
{
    var plot = Plot.SingleOrDefault(p => p.UserID == LoggedInUser.ID && p.UrlFriendlyName == name);
    // ViewData["plot"] = plot;
    return View(plot);
}

该视图上的表单发回以下控制器操作:

[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult Edit(Plot plot)
{
    var validator = new PlotValidator();
    try
    {
        var results = validator.Validate(plot);
        if (!results.IsValid)
        {
            ...
        }
    }
}

我遇到的问题是,第一个控制器操作实际上并不使用提交给表单的 Plot 对象中的值填充表单。我可以让它填充表单的唯一方法是将 Plot 添加到 ViewData:

ViewData["plot"] = plot;

这会填充编辑表单,但是当我编辑值并将其提交回上面列出的第二个控制器操作时,它基本上只是发回具有新值的新图,而不是发送到具有更新值的编辑表单的图。

我确信我可能只是错过了一些简单的东西,但我似乎无法弄清楚它是什么。我的表单上的所有字段都有以“plot”为前缀的 ID。

有人知道/看到我做错了什么吗?谢谢。

I'm using ASP.NET MVC (1.0), Spark View Engine (1.0) and SubSonic (3.0.0.3).

I'm having some trouble getting an "Edit" view to work correctly. So, in my controller, I have the following controller action that displays the edit form view:

[AcceptVerbs(HttpVerbs.Get)]
public virtual ActionResult Edit(string name)
{
    var plot = Plot.SingleOrDefault(p => p.UserID == LoggedInUser.ID && p.UrlFriendlyName == name);
    // ViewData["plot"] = plot;
    return View(plot);
}

The form on that view posts back to the following controller action:

[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult Edit(Plot plot)
{
    var validator = new PlotValidator();
    try
    {
        var results = validator.Validate(plot);
        if (!results.IsValid)
        {
            ...
        }
    }
}

The problem that I'm having, is that the first controller action doesn't actually populate the form with the values from the Plot object submitted to it. The only way I can get it to populate the form is by adding the Plot to ViewData:

ViewData["plot"] = plot;

That populates the edit form, but when I edit the values, and submit it back to the second controller action listed above, it just sends back, basically a new plot with the new values, not the plot sent to the edit form with updated values.

I'm sure I'm probably just missing something simple, but I can't seem to figure out what it is. All of the fields on my form have IDs that are prefixed with "plot."

Anyone know/see what I'm doing wrong? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

用心笑 2024-08-11 19:15:08

我不知道它在Spark视图引擎中是如何完成的,但是在ASP.NET MVC附带的开箱即用视图引擎中,这是通过继承页面中的强类型视图模型对象来完成的页面指​​令,而不是使用普通 ViewPage 附带的字典。

因此,视图中的第一行代码不会像这样:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage" %>

它将看起来更像这样:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
     Inherits="System.Web.Mvc.ViewPage<HomePageViewModel>" %>

您当前的控制器代码需要一个强类型对象,但它会返回一个 ViewData 字典。所以没有发生绑定。

考虑查看以下播客:

探索如何将 ViewData(强类型和弱类型)与 Spark 结合使用
http://www.dimecasts.net/Casts/CastDetails/117

I don't know how it's done in the Spark view engine, but in the out-of-the-box view engine that comes with ASP.NET MVC, this is done by inheriting a strongly-typed view model object in the page using a page directive, rather than using the dictionary that comes with the plain-vanilla ViewPage.

So, instead of the first line of code in the view looking like this:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage" %>

It will look more like this:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
     Inherits="System.Web.Mvc.ViewPage<HomePageViewModel>" %>

Your current controller code is expecting a strongly-typed object, but it is getting back a ViewData Dictionary instead. So no binding is occurring.

Consider reviewing the following podcast:

Exploring how to use ViewData (Strongly typed and weak typed) with Spark
http://www.dimecasts.net/Casts/CastDetails/117

魂归处 2024-08-11 19:15:08

嘿罗伯特 - 我不知道 Spark 如何处理它的数据,但我可以告诉你第二部分很容易修复。

首先 - 您必须记住 MVC 不会为您访问数据库。在您的情况下,您提取记录的标准似乎涉及两个标准 - 因此,如果您想确保数据设置正确,则必须再次提取该条件。因此,在 POST 上将您的签名更改为:

[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult Edit(string id, FormCollection form)
{
//assuming you have some kind of PK
var plot = Plot.SingleOrDefault(p => p.ID == id);    
UpdateModel(plot, form.ToValueProvider());
var validator = new PlotValidator();
    try
    {
        var results = validator.Validate(plot);
        if (!results.IsValid)
        {
            ...
        }
    }
}

这是徒手的 - 但无论如何您都必须使用 L2S 来做到这一点......

Hey Robert - I don't know how Spark handles it's data, but I can tell you that the second part is pretty simple to fix.

First - you have to remember MVC won't reach into the DB for you. In your case your criteria for pulling out a record seems to involve two criteria - so you'll have to pull that again if you want to make sure the data is set properly. So on POST change your signature to this:

[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult Edit(string id, FormCollection form)
{
//assuming you have some kind of PK
var plot = Plot.SingleOrDefault(p => p.ID == id);    
UpdateModel(plot, form.ToValueProvider());
var validator = new PlotValidator();
    try
    {
        var results = validator.Validate(plot);
        if (!results.IsValid)
        {
            ...
        }
    }
}

This is freehanded - but it's how you have to do it with L2S anyway...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文