Click here to Skip to main content
14,882,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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")
Posted
Updated 2-May-16 23:20pm

1 solution

Yes it is possible

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

example:

C#
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
JavaScript
@model  MyApp.Controllers.ModelContainer


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

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900