asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – ASP.NET MVC – 查看多个模型大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图生成这样的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);
}

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

大佬总结

以上是大佬教程为你收集整理的asp.net-mvc – ASP.NET MVC – 查看多个模型全部内容,希望文章能够帮你解决asp.net-mvc – ASP.NET MVC – 查看多个模型所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。