MVC更新模型

发布于 2024-07-24 03:42:07 字数 1096 浏览 5 评论 0原文

我不确定以前是否有人问过这个问题,但现在就这样。

我有一个 MVC 应用程序,其 HTML 如下所示;

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

我在控制器中有以下内容;

    [Authorize]
    public ActionResult Edit(int? id)
    { snip }

然后在提交按钮事件上;

    [AcceptVerbs(HttpVerbs.Post)]
    [Authorize]
    public ActionResult Edit(FormCollection collection)
    {
        CalendarItem fvm = new CalendarItem();

        UpdateModel(fvm);
    }

如果我更新现有的活动,我没有问题。 如果我添加新事件,则会收到一条错误,指出 UpdateModel 无法更新模型。

如果我从第一个 ActionResult 中删除“int?id”参数,则模型会根据新事件和现有事件进行更新。

当我编辑事件时,我使用以下 HTML; <%=Html.ActionLink("Edit", "Edit", new {id=Model.Event.id}) %> 当我创建新事件时,我使用 <%=Html.ActionLink("添加事件","编辑", "日历") %>.

现在,无可否认,我可能不应该使用相同的视图来更新和创建,也许应该重构为两个视图和一个 PartialView。

那么,这是解决方案还是我做错了什么?

提前致谢。

I'm unsure if this has been asked before but here goes.

I have an MVC application with the HTML looking like this;

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

I have in the controller the following;

    [Authorize]
    public ActionResult Edit(int? id)
    { snip }

Then on a submit button event;

    [AcceptVerbs(HttpVerbs.Post)]
    [Authorize]
    public ActionResult Edit(FormCollection collection)
    {
        CalendarItem fvm = new CalendarItem();

        UpdateModel(fvm);
    }

If I am updating an existing event I have no problems. If I am adding a new event I get an error that UpdateModel failed to update the model.

If I remove the "int? id" parameter from the first ActionResult the model is updated on both new and existing events.

When I am editing an event I use the following HTML; <%=Html.ActionLink("Edit", "Edit", new {id=Model.Event.id}) %> and when I am creating a new event I use <%=Html.ActionLink("Add event","Edit", "Calendar") %>.

Now admitably I probably shouldn't be using the same View for both update and create and should perhaps refactor into two views and a PartialView.

So, is that the solution or is there something else I am doing wrong?

Thanks in advance.

</griegs>

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

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

发布评论

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

评论(2

惟欲睡 2024-07-31 03:42:08

好吧,事实证明你确实应该关注点分离。

我创建了“添加”和“编辑”操作,将编辑屏幕转换为部分视图并添加了“添加”视图。

一切都很完美。

我想关于 mvc 以下内容是正确的。 “当事情变得困难时,你可能做错了”。

OK, turns out you really should have seperation of concerns.

I created Add and Edit actions, converted the edit screen into a partial view and added an Add view.

All works perfectly.

I guess with regards to mvc the following is true. "When it becomes hard you're probably doing it wrong".

冬天旳寂寞 2024-07-31 03:42:08

我看不到您的其余代码,但我假设在编辑时您将现有记录传递到视图中。 当您尝试发布具有相同 ID 的新记录时,控制器会认为您正在尝试修改现有记录而不是创建新记录。

我认为您可以使用相同的视图进行编辑和添加,但是在添加时,您必须将新创建的记录对象传递到您的视图。 对于我来说,更容易发布到两种不同的控制器方法(一种用于添加,一种用于编辑),并在那里完成繁重的工作。

I can't see the rest of your code, but I am assuming that on an edit you are passing an existing record into the view. When you try to post a new record with the same ID, the controller thinks you are trying to modify the existing record instead of creating a new one.

I think you can use the same view for both edits and adds, but on an add you will have to pass in a newly'created record object to your view. For my money it's easier to post to two different controller methods (one for add, one for edit), and do the grunt work there.

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