过帐到“编辑"控制器操作未传递模型ID [英] Posting to Edit controller action not passing ID of model

查看:53
本文介绍了过帐到“编辑"控制器操作未传递模型ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器上执行了以下操作:

  [HttpPost]公共ActionResult编辑(EditMyObjectViewModel editMyObjectViewModel){} 

EditMyActionViewModel 包含 MyObject

这将传递到编辑"视图(上述控制器操作的GET版本)

将其重新发布后,未设置ID....
如果我将控制器更改为:

  [HttpPost]公共ActionResult编辑(向导ID,EditMyObjectViewModel editMyObjectViewModel){editMyObjectViewModel.ID = id;} 

那行得通,但是似乎有点不对吗?

我想我也可以将视图上的隐藏字段绑定到Model.ID?

这里的惯例是什么?

编辑

模型/视图模型如下:

 公共类EditMyObjectViewModel{公共IEnumerable< SomeItem>SomeUnrelatedStuff {get;放;}公共MyObject MyObject {get;放;}}公共类MyObject{公共Guid ID {get;放;}公共字符串名称{get;放;}} 

视图如下:

  @model MyApp.Models.EditMyObjectViewModel@using(Html.BeginForm("Edit","License",FormMethod.Post,新{@class ="form form-horizo​​ntal"})){@ Html.TextboxFor(x => x.MyObject.Name);< input type ="submit" class ="btn btn-success" value =创建修改"/>} 

解决方案

是的,您要么需要创建一个隐藏字段来保存ID,要么需要将ID作为参数添加到操作方法中.

问题只是ID的来源以及填充方式.默认的模型装订器从表单字段填充模型.它不会使用查询参数.而且查询参数不会进入模型,但会填充到action方法的参数中.

您可以做的另一件事是创建一个自定义模型绑定程序,以将查询参数填充到模型中.但是,在这种情况下,这感觉有些过分了.另外,您必须为每个页面/模型执行此操作.

是我,我将id添加到action方法中.

I've got an action on my controller that looks like this:

[HttpPost]
public ActionResult Edit(EditMyObjectViewModel editMyObjectViewModel)
{
}

EditMyActionViewModel contains a MyObject

This is passed in to the Edit view (the GET version of the above controller action)

When it is posted back in, the ID isn't set....
If I change the controller to be:

[HttpPost]
public ActionResult Edit(Guid id, EditMyObjectViewModel editMyObjectViewModel)
{
    editMyObjectViewModel.ID = id;
}

That works, but it seems a little wrong?

I guess I could also bind a hidden field on the view to Model.ID?

What's the convention here?

EDIT

Model / ViewModels are as follows:

public class EditMyObjectViewModel
{
    public IEnumerable<SomeItem> SomeUnrelatedStuff { get; set; }
    public MyObject MyObject { get; set; }
}

public class MyObject
{
    public guid ID { get; set; }
    public string Name { get; set; }
}

View is as follows:

@model MyApp.Models.EditMyObjectViewModel
@using (Html.BeginForm("Edit", "License", FormMethod.Post, new { @class = "form form-horizontal" }))
{
        @Html.TextboxFor(x=>x.MyObject.Name);
        <input type="submit" class="btn btn-success" value="Create Modification" />
}

解决方案

You are right, you either need to create a hidden field to hold the id or you need to add the ID as a parameter to the action method.

The issue is simply where the id comes from and how it gets populated. The default model binder populates the model from form fields; it wont' use a query parameter. And the query parameter won't go into the model but it will get populated in an argument on the action method.

The one other thing you could do is to create a custom model binder to populate the query parameter into the model. But that feels like overkill in this situation. Plus, you'd have to do this for each page/model.

It it was me, I'd add the id to the action method.

这篇关于过帐到“编辑"控制器操作未传递模型ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆