是否可以在ASP.NET MVC中传递两个模型进行查看 [英] Is it possible to pass two model to view in ASP.NET MVC

查看:67
本文介绍了是否可以在ASP.NET MVC中传递两个模型进行查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有选项卡的布局。我正在使用asp.net mvc。我创建了onE模型,其中我有另一个模型用于第二个选项卡。然后我有存储库文件夹。我在其中调用存储的porcedure将数据发送到后端。

i想问一个视图中有2个模型可以吗?



我尝试过的方法:



公共类股票控制器:控制器

{

公共ActionResult指数()

{



返回查看();

}

public ActionResult StockDetails()

{

返回查看();

}





[HttpPost]

public ActionResult StockDetails(StockModel s,StockModel.materialModel m)

{



尝试

{

if(ModelState.IsValid)

{

StockRepository Sr = new StockRepository();







if(Sr.Stock(s))

{

ViewBag.Message =股票详情成功添加;



}

如果(Sr.Material(m))

{

ViewBag.Message =材料细节成功添加;





}





}



返回查看();

}

catch

{

返回查看();

}





}}}





这是我的模型:

使用System;

使用System.Collections.Generic;

使用System.Linq ;

使用System.Web;



命名空间MathuraApp.Models

{

公共类StockModel
{



public string Voucher_No {get;组; }

public string Date {get;组; }

public string Currency {get;组; }

公共字符串Transfer_From {get;组; } $ / $
公共字符串Transfer_To {get;组; }











public class materialModel

{

public string Voucher_No {get;组; }

public string Date {get;组; }

public string Currency {get;组; }

公共字符串Receive_From {get;组; }

public string Receive_To {get;组; }



}

}

}







这是我的观点

@model MathuraApp.Models.StockModel

@ * @ model MathuraApp .Models.StockModel.materialModel * @





@ {

ViewBag.Title =Stock;

布局=〜/ Views / Shared / _AccountLayout.cshtml;

}



股票





@using(Html.BeginForm())

{

@Html。 AntiForgeryToken()











CurrencyCreationmodel






@ Html.ValidationSummary(true,,new { @class =text-danger})



@ Html.LabelFor(model => model.Voucher_No,htmlAttributes :new {@class =control -label col-md-2})



@ Html.EditorFor(model => model.Voucher_No,new {htmlAttributes = new {@class =form-control}})

@ Html.ValidationMessageFor(model => model.Voucher_No,,new {@class = text-danger})









@ Html.LabelFor(model => model.Date,htmlAttributes:new {@class =control-label col-md-2})



@ Html.EditorFor(model => model.Date,new {htmlAttributes = new {@class =form-控制}})

@ Html.ValidationMessageFor(model => model.Date,,new {@class =text-danger})









@ Html.LabelFor(model => model.Transfer_From,htmlAt致敬:新{@class =control-label col-md-2})



@Html。 EditorFor(model => model.Transfer_To,new {htmlAttributes = new {@class =form-control}})

@ Html.ValidationMessageFor(model => model.Transfer_To,,new {@class = text-danger})









@ Html.LabelFor(model => model.Currency,htmlAttributes:new {@class =control-label col-md-2})



@ Html.EditorFor(model => model.Currency,new {htmlAttributes = new {@class =form-控制}})

@ Html.ValidationMessageFor(model => model.Currency,,new {@class =text-danger})
















}





@ Html.ActionLink(返回列表,索引)

解决方案

是的可能



参考这篇文章

多个模型MVC中的单一视图 [ ^ ]



例如:



  public   class  ModelContainer 
{
public Model1 Model1Property { get ; set ; }
public Model1 Model2Property { get ; set ; }
}

public class Model1
{
public int Property1 { get ; set ; }
public string Property2 { get ; set ; }
}

public class Model2
{
public int Property1 { get ; set ; }
public string Property2 { get ; set ; }
}



public class HomeController:Controller
{
public ActionResult Index()
{
ModelContainer obj = new ModelContainer();
obj.Model1Property = new Model1(){Property1 = 1 ,Property2 = some};
obj.Model2Property = new Model1(){Property1 = 1 ,Property2 = some};
return 查看(obj);
}





查看

 @model MyApp.Controllers.ModelContainer 


< span> @ Model.Model1Property.Property1 < / span >

< span> @ Model.Model2Property.Property2 < / span >


i have layout with tabs. i am using asp.net mvc. i have created onE model in which i have another model for second tab. then i have repository folder. in which i am calling stored porcedure to send data to backend.
i want to ask "is it possible to have 2 models in one view?"

What I have tried:

public class StockController : Controller
{
public ActionResult Index()
{

return View();
}
public ActionResult StockDetails()
{
return View();
}


[HttpPost]
public ActionResult StockDetails(StockModel s,StockModel.materialModel m)
{

try
{
if (ModelState.IsValid)
{
StockRepository Sr = new StockRepository();



if (Sr.Stock(s))
{
ViewBag.Message = "stock details added successfully";

}
if (Sr.Material(m))
{
ViewBag.Message = "material details added successfully";


}


}

return View();
}
catch
{
return View();
}


}}}


this is my model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MathuraApp.Models
{
public class StockModel
{

public string Voucher_No { get; set; }
public string Date { get; set; }
public string Currency { get; set; }
public string Transfer_From { get; set; }
public string Transfer_To { get; set; }





public class materialModel
{
public string Voucher_No { get; set; }
public string Date { get; set; }
public string Currency { get; set; }
public string Receive_From { get; set; }
public string Receive_To { get; set; }

}
}
}



this is my view
@model MathuraApp.Models.StockModel
@*@model MathuraApp.Models.StockModel.materialModel*@


@{
ViewBag.Title = "Stock";
Layout = "~/Views/Shared/_AccountLayout.cshtml";
}

Stock



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





CurrencyCreationmodel




@Html.ValidationSummary(true, "", new { @class = "text-danger" })


@Html.LabelFor(model => model.Voucher_No, htmlAttributes: new { @class = "control-label col-md-2" })


@Html.EditorFor(model => model.Voucher_No, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Voucher_No, "", new { @class = "text-danger" })





@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })


@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })





@Html.LabelFor(model => model.Transfer_From, htmlAttributes: new { @class = "control-label col-md-2" })


@Html.EditorFor(model => model.Transfer_To, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Transfer_To, "", new { @class = "text-danger" })





@Html.LabelFor(model => model.Currency, htmlAttributes: new { @class = "control-label col-md-2" })


@Html.EditorFor(model => model.Currency, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Currency, "", new { @class = "text-danger" })









}


@Html.ActionLink("Back to List", "Index")

解决方案

Yes it is possible

refer this article
Multiple Models in Single View in MVC[^]

example:

public class ModelContainer
    {
        public Model1 Model1Property { get; set; }
        public Model1 Model2Property { get; set; }
    }

    public class Model1
    {
        public int Property1 { get; set; }
        public string Property2 { get; set; }
    }

    public class Model2
    {
        public int Property1 { get; set; }
        public string Property2 { get; set; }
    }



    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ModelContainer obj = new ModelContainer();
            obj.Model1Property = new Model1() { Property1 = 1, Property2 = "some" };
            obj.Model2Property = new Model1() { Property1 = 1, Property2 = "some" };
            return View(obj);
        }



View

@model  MyApp.Controllers.ModelContainer


<span> @Model.Model1Property.Property1</span>

<span> @Model.Model2Property.Property2</span>


这篇关于是否可以在ASP.NET MVC中传递两个模型进行查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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