Click here to Skip to main content
15,741,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a model which is normal a class just for a strongly typed view.

I am stuck.I want to show two model data in one View.one is simple and the other is coming from database (checkBoxList ) one.

Pls help.

What I have tried:

C#
public class ProcessSelectDate
   {
       [Required]
       public string fromdate { get; set; }
       [Required]
       public string Todate { get; set; }
       //Processes
   }

and another model that is through Entity framework

C#
public partial class Process
   {
       public decimal id { get; set; }
       public string ProcessName { get; set; }

       public bool Ischecked { get; set; }
   }


I want to show the checkboxlist of processname in The view which is coming from database.

so my view will be

for textbox and checkboxList, moreover two models

HTML
@Html.TextBoxFor(model => model.FromDate)
      </div> <br />
      <div><span class="RequiredField">*&nbsp;</span>To Date:</div>
      <div>
          @Html.TextBoxFor(model => model.ToDate)
      </div> <br />



@for (int i = 0; i < Model.processName.Count; i++)
{

@Html.HiddenFor(x => x.processName[i].Name)
@Html.CheckBoxFor(x => x.processName[i].Checked)
@Html.LabelFor(x => x.processName[i].Checked, Model.processName[i].Name)

}

and in controller
dbcontext db = new dbcontext();
db.Processes.ToList(); //
Posted
Updated 11-Aug-16 3:27am
Comments
Karthik_Mahalingam 11-Aug-16 8:33am    
refer this
http://www.codeproject.com/Answers/1097481/Is-it-possible-to-pass-two-model-to-view-in-ASP-NE#answer1
Richard Deeming 11-Aug-16 9:21am    
You should post this as a solution. But post a copy of your previous answer, rather than a link to it, just in case the other question is ever deleted.
F-ES Sitecore 11-Aug-16 9:31am    
If you'd googled "mvc view two models" you would have found your answer in seconds.

1 solution

Posting as Solution From Richards Comment

Controller
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

@model  MyApp.Controllers.ModelContainer 

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



refer: Multiple Models in Single View in MVC[^]
 
Share this answer
 
Comments
rajshree748 12-Aug-16 7:19am    
Thank you so much..It is going to work..
Karthik_Mahalingam 12-Aug-16 7:37am    
welcome
rajshree748 12-Aug-16 7:59am    
Can you pls tell me how to bind checkbox from Database without using any Editormodel
Karthik_Mahalingam 12-Aug-16 8:03am    
post the code.
to input tag?

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