如何让 ASP.NET MVC 的 UpdateModel 忽略主键列?

发布于 2024-08-06 11:32:06 字数 881 浏览 1 评论 0原文

我有一个带有 GUID 主键的表,我尝试使用 ASP.NET MVC 控制器的 UpdateModel 方法插入新创建的对象(db 是 LINQ-to-SQL 数据上下文) :

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection collection)
    {
        Fields field = new Fields();
        field.ID = Guid.NewGuid();

        try
        {
            UpdateModel(field);

            db.Fields.InsertOnSubmit(field);
            db.SubmitChanges();

            return RedirectToAction("Index");
        }
        catch
        {
            return View("Edit", field);
        }
    }

首先,模型上用于编辑和创建的自动生成视图包含一个供用户编辑的主键字段似乎很奇怪,但删除这些字段很容易。

但我遇到了一个问题,当主键未作为表单集合的一部分传入时,对 UpdateModel 的调用会引发异常。

我是否缺少某种将主键列标记为用户(以及扩展的对 UpdateModel 的调用)不应该乱搞的方法?我可以添加一个 includeProperties 数组并保留主键的名称,但这不是很干燥)。

我还可以在包含主键的表单中添加一个隐藏字段——使用 GUID 主键,我可以在 Create 的 GET 请求上执行此操作。

I have a table with a GUID primary key into which I'm trying to insert a newly-created object using an ASP.NET MVC Controller's UpdateModel method (db is a LINQ-to-SQL data context):

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection collection)
    {
        Fields field = new Fields();
        field.ID = Guid.NewGuid();

        try
        {
            UpdateModel(field);

            db.Fields.InsertOnSubmit(field);
            db.SubmitChanges();

            return RedirectToAction("Index");
        }
        catch
        {
            return View("Edit", field);
        }
    }

First off, it seems odd that the auto-generated views for Edit and Create on the model would include a primary key field for the user to edit, but it's easy enough to delete those.

But I'm running into a problem where the call to UpdateModel throws an exception when the primary key is not passed in as part of the form collection.

Am I missing some way of flagging the primary key column as something that the user (and by extension, the call to UpdateModel) shouldn't be mucking with? I could add an includeProperties array and leave the primary key's name out, but that's not very DRY).

I could also add a hidden field to my form incorporating the primary key -- with a GUID primary key I can do this on the GET request to Create.

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

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

发布评论

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

评论(2

宁愿没拥抱 2024-08-13 11:32:06

当您不更新模型对象时,请勿使用 UpdateModel。在这种情况下,您正在创建一个... ScottGu 在 这篇博文

如果您想要一种更简单的数据绑定方法,如果您使用正确的命名约定,ASP.NET MVC 将自动为您完成大量工作。例如,具有输入字段“firstName”、“lastName”和“birthDate”的表单可以将其数据发送到具有以下签名的操作方法:

public ActionResult Create(string firstName, string lastName, DateTime birthDate)

并且参数将使用表单值填充。请注意,数据类型不必是字符串 - 事实上,您甚至可以绑定到您自己的类。如果您有这个类...

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Birthdate { get; set; }
}

...以及一个包含输入字段“person_FirstName”、“person_LastName”和“person_BirthDate”的表单,您可以立即将其绑定到一个新的 Person 对象:

public ActionResult Create(Person person) { ... }

很好,是吧? =)

Don't use UpdateModel when you're not updating a model object. In this case, you're creating one... ScottGu has some nice examples in this blog post.

If you want an easier way to databind, ASP.NET MVC will automatically do a lot of the work for you, if you use the right naming conventions. For example, a form with the input fields "firstName", "lastName" and "birthDate" could send its data to an action method with the following signature:

public ActionResult Create(string firstName, string lastName, DateTime birthDate)

and the parameters would be populated with the form values. Note that the data type doesn't have to be a string - in fact, you can even bind to your own classes. If you have this class...

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Birthdate { get; set; }
}

...and a form with the input fields "person_FirstName", "person_LastName" and "person_BirthDate" you can bind it to a new Person object immediately:

public ActionResult Create(Person person) { ... }

Nice, huh? =)

浅唱ヾ落雨殇 2024-08-13 11:32:06

首先:在这种情况下,具有主键的属性应该是只读的。

您还可以使用自己的 BindModel 类。

First of all: Property with Primary Key should be read-only in that case.

You can also use your own BindModel class.

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