1

我有一个带有自定义对象的视图模型。在最初的获取中,我填充了 Foo 并使用了 Foo 的几个属性。

在帖子中,我发现视图模型上的 Foo 为空。

我可以添加到我的视图中,@Html.HiddenFor(x => x.Foo.Id)这可以确保 Foo 至少填充了一个 Id,但是我可能需要为所有属性添加类似的代码。

有没有办法发回完整的对象?

public class RequestModel
    {
        public Foo Foo{ get; set; }

        [Required]
        [Display(Name = "Comment")]
        public string Comment { get; set; }
    }

控制器

public ActionResult Index(int? id)
{
   //Populate Foo here using EF and add it to the model
   var model = new RequestModel { Foo = foo };    
   return View(model);
}



[HttpPost]
public ActionResult Index(int? id, RequestModel model)
{
   return View(model);
}

看法

@Html.DisplayTextFor(m=>m.Application.Name)

ETC

4

1 回答 1

1

将具有所需属性的视图模型添加到解决方案中。将您的验证等放在上面,并使用它在您的页面和控制器之间移动您的数据,然后将属性映射到您的 EF 对象。

于 2012-11-23T03:20:25.560 回答