ASP.NET MVC - 查看多个模型 [英] ASP.NET MVC - View with multiple models

查看:82
本文介绍了ASP.NET MVC - 查看多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想产生这样的HTML

I am trying to generate such 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>

所以它可以用这个行动来处理:

so it can be processed by this Action:

    public ActionResult Process(Foo foo, Bar bar)
    {
        ...
    }

由于动作code

Given the Action code

    public ActionResult Edit()
    {
        ViewData["foo"] = new Foo { A = "aaa" };
        ViewData["bar"] = new Bar { B = "bbb" };

        return View();
    }

我应该怎么写Edit.aspx看法?我不想写名字foo.a'和'bar.b手动。

what should I write in Edit.aspx view? I don't want to write names 'foo.a' and 'bar.b' manually.

推荐答案

字符串索引的ViewData 是坏的。你可能想要做的是为您的多变量视图数据有点包装类和它传递给一个强类型的视图。 IE浏览器:

String-indexed ViewData is bad. What you probably want to do is make a little wrapper class for your multi-variable view data and pass that to a strongly typed view. 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 键,你可以使用调用该对象的成员型号属性

这篇关于ASP.NET MVC - 查看多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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