如何在一个文件中使用Model和viewModel?

时间:2015-10-08 00:59:39

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 model-view-controller asp.net-mvc-5

我是一名学习MVC的新手。

我在文件中有以下代码

@model  MVCLearn.Models.Customer
@{
    Layout = null;
}    
<!DOCTYPE html>    
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>entercustomer</title>
</head>V
<body>
    <div> 
        @using (Html.BeginForm("submit","Customer",FormMethod.Post))
        {
    <i> Customer code:-</i>
            @Html.TextBoxFor(m=>m.customercode) <br/>
            @Html.ValidationMessageFor(x => x.customercode) <br/>
    <i> Customer name</i> @Html.TextBoxFor(m => m.customername)
            @Html.ValidationMessageFor(x => x.customername) <br/>
            <input id="Submit1" type="submit" value="submit" />
        }
        <br/>         
    </div>
</body>
</html>

现在我想将viewModel添加到同一页面,例如:如果客户名称不以大写字母开头,则需要显示消息。
我在viewmodel MVCLearn.Models.CustomerVM中编写了一个逻辑。  如何将viewmodel添加到此? 当我添加相同的视图我得到错误&#34;只有一个型号&#39;语句允许在文件中。&#34; 如何在文件中添加模型和视图模型?

请帮忙。

1 个答案:

答案 0 :(得分:1)

您可以在MVC中使用Tuple,如下面的代码......

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var first = new FirstModel();
        var second = new SecondModel();

        return View(Tuple.Create(first,second));
    }
}

@model Tuple<FirstModel, SecondModel>

<div> 
    @Model.Item1.FirstModelProp
    @Model.Item2.SecondModelProp
</div>

我认为它会对你有所帮助。

相关问题