-2

I have model which have multiple other models in it.

public class mainmodel
{
 public entity1 ();
 public entity2 ();
}

After posting view, I get entity1 entities null? Any luck. I am doing wrong? My question is: Asp.net with MVC multiple model in one view (create, update) How can I get those class entities in model?

5
  • Please, show your view and controller code. Commented Mar 13, 2013 at 6:54
  • In my view, I am rendering partial view withpassing entity1 and entity2. So can I get values of model after posting Commented Mar 13, 2013 at 6:55
  • Check This Commented Mar 13, 2013 at 6:58
  • But I have single model and multiple entities class wise. So I created one model and all entity classes in it. Commented Mar 13, 2013 at 7:01
  • My question is: stackoverflow.com/questions/5267152/… Commented Mar 13, 2013 at 7:26

2 Answers 2

0

Controller(HomeController) :

public ActionResult Index()
    {
        mainmodel model=new mainmodel();
        return View(model);
    }

[HttpPost]
public ActionResult Index(mainmodel model)
    {
        return View(model);
    }

View(Home/Index) :

@model mainmodel
@using (Html.BeginForm("Index","Home",FormMethod.Post))
 {
   <input type="submit" value="Submit" />
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, But I have another class in IndexPageViewModel and that is not working.
You cannot bind multiple model with one view in mvc, because it is 1:1 relationship. If you want to that, you have to make a ViewModel, which contains the model which you want to bind in your view.See the code in my next answer.I hope it helps.
0

ViewModel(IndexViewModel) :

public class IndexViewModel
{
   public mainmodel model1 {get;set;}
   public anotherclass model2 {get;set;}
}

Controller(HomeController) :

public ActionResult Index()
 {
    IndexViewModel model=new IndexViewModel();
    return View(model);
 }

[HttpPost]
public ActionResult Index(IndexViewModel model)
 {
    return View(model);
 }

View(Home/Index) :

@model IndexViewModel
@using (Html.BeginForm("Index","Home",FormMethod.Post))
{
  <input type="submit" value="Submit" />
}

now you can get all the value in HttpPost.

2 Comments

Hi, Above works but as I said, I am passing MainViewmodel.Entityclass in partial view. @Html.Partial("_Model1", Model.MyModel1) and it gives error. The model item passed into the dictionary is of type 'MvcApplication1.Models.MainModel', but this dictionary requires a model item of type 'MvcApplication1.Models.Model1'.
Which model you want to pass into your partial view, you need to declare exact same model on the top of the page(partial view).I think you write at the top of the Partial view "@model MvcApplication1.Models.MainModel" instead of "@model MvcApplication1.Models.Model1", which is wrong.Please write "@model MvcApplication1.Models.Model1" at the top of your partial view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.