我试图生成这样的HTML
- <form action="/some/process" method="post">
- <input type="hidden" name="foo.a" value="aaa"/>
- <input type="hidden" name="bar.b" value="bbb"/>
- <input type="submit" />
- </form>
所以它可以通过这个动作处理:
- public ActionResult Process(Foo foo,Bar bar)
- {
- ...
- }
给出Action代码
- public ActionResult Edit()
- {
- ViewData["foo"] = new Foo { A = "aaa" };
- ViewData["bar"] = new Bar { B = "bbb" };
- return View();
- }
我应该在Edit.aspx视图中写什么?我不想手动写名字’foo.a’和’bar.b’。
解决方法
字符串索引ViewData是坏的。你可能想要做的是为你的多变量视图数据做一个小包装类,并传递给一个强类型视图。 IE:
- public class FooBarViewData
- {
- public Foo Foo {get; set;}
- public Bar Bar {get; set;}
- }
- public ActionResult Edit()
- {
- FooBarViewData fbvd = new FooBarViewData();
- fbvd.Foo = new Foo(){ A = "aaa"};
- fbvd.Bar = new Bar(){ B = "bbb"};
- return View(fbvd);
- }