asp.net-mvc – ASP.NET MVC – 查看多个模型

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – ASP.NET MVC – 查看多个模型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图生成这样的HTML
  1. <form action="/some/process" method="post">
  2. <input type="hidden" name="foo.a" value="aaa"/>
  3. <input type="hidden" name="bar.b" value="bbb"/>
  4. <input type="submit" />
  5. </form>

所以它可以通过这个动作处理:

  1. public ActionResult Process(Foo foo,Bar bar)
  2. {
  3. ...
  4. }

给出Action代码

  1. public ActionResult Edit()
  2. {
  3. ViewData["foo"] = new Foo { A = "aaa" };
  4. ViewData["bar"] = new Bar { B = "bbb" };
  5.  
  6. return View();
  7. }

我应该在Edit.aspx视图中写什么?我不想手动写名字’foo.a’和’bar.b’。

解决方法

字符串索引ViewData是坏的。你可能想要做的是为你的多变量视图数据做一个小包装类,并传递给一个强类型视图。 IE:
  1. public class FooBarViewData
  2. {
  3. public Foo Foo {get; set;}
  4. public Bar Bar {get; set;}
  5. }
  6. public ActionResult Edit()
  7. {
  8. FooBarViewData fbvd = new FooBarViewData();
  9. fbvd.Foo = new Foo(){ A = "aaa"};
  10. fbvd.Bar = new Bar(){ B = "bbb"};
  11. return View(fbvd);
  12. }

然后你的视图只是强烈打字为FooBarViewData,你可以使用Model属性调用该对象的成员。

猜你在找的asp.Net相关文章