【问题标题】:MVC strongly typed view dataMVC 强类型视图数据
【发布时间】:2016-03-17 09:11:34
【问题描述】:

我有一个强类型视图,可以创建这样的表单字段:

@Html.TextBoxFor(x => x.Name, new { @class = "form-control" })

没关系,上面 lambda 表达式中的 X 与我创建的具有“名称”属性的类有关。

我如何使用相同的视图但可以访问不同类的属性?例如,如果您想象我有另一个名为 - UserDetails 的类,而电子邮件地址就是它的一个属性。我该怎么做:

@Html.TextBoxFor(x => x.Email, new { @class = "form-control" })

在同一个强类型视图中?

【问题讨论】:

  • 您使用调用相应视图的操作将新模型沿新视图传递,就可以了!
  • 我不确定你的意思,我如何将新模型沿新视图传递?
  • 你可以拥有两个模型共同属性的基类,然后为此创建一个视图,然后在你的视图中你可以转换类型并加载另一个部分视图

标签: asp.net-mvc


【解决方案1】:

在 asp.net mvc 中,您将模型传递给这样的视图:

    public ActionResult Base()
        {
            return View(new DerviedOne());
        }

这是你的模型定义:

  public class BaseModel
    {
        public int Id { get; set; }
    }

    public class DerviedOne : BaseModel
    {
        public string Email { get; set; }
    }

    public class DerviedTwo : BaseModel
    {
        public string Name { get; set; }
    }

然后你必须创建三个视图:

基础视图:

@using Models
@model Models.BaseModel

@{
    ViewBag.Title = "Base";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Base</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>BaseModel</h4>
        <hr/>
        @Html.ValidationSummary(true, "", new {@class = "text-danger"})
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                @Html.TextBoxFor(x=>x.Id)
                <input type="submit" value="Create" class="btn btn-default"/>
            </div>
        </div>
    </div>

    if(Model is DerviedOne)
     {
         Html.RenderPartial("DerviedOneView", Model as DerviedOne);
     }

    if (Model is DerviedTwo)
    {
        Html.RenderPartial("DerviedTwoView", Model as DerviedTwo);
    }
}

第二个视图:

@model WebApplication.Models.DerviedOne

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>DerviedOne</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

第三视图:

@model WebApplication.Models.DerviedTwo

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>DerviedTwo</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    相关资源
    最近更新 更多