0

I have an ASP/MVC view that displays data from a model and also contains a partial view with a form that uses a different model. Is there a way for me to combine these into one view? The result would be that the display values are based on model A but the form in the page submits model B. Is that possible?

3
  • Create a viewmodel which exposes the models needed by the view as properties e.g. myViewModel.CustomerModel.CustomerName and myViewModel.OrderModel.OrderNumber etc... Commented May 18, 2015 at 14:24
  • If it's ASP.NET and not Classic ASP, please always indicate that it is ASP.NET, since they are very different. I already know you mean ASP.NET, but a novice may not. Commented May 18, 2015 at 14:25
  • I added a tag for asp.NET... I try not to put that stuff in the title/question because some jerk moderator likes to leave irritating comments about how my questions don't follow some set of rules. Commented May 18, 2015 at 14:46

2 Answers 2

1

If the two things are closely related, you can use a view model to work with both in your view:

public class FooViewModel
{
    public SomeModelForDisplay Foo { get; set; }
    public SomeModelForForm Bar { get; set; }
}

In your action, initialize both:

public ActionResult Foo(int id)
{
    var foo = db.Foos.Find(id);
    if (foo == null)
    {
        return new HttpNotFoundResult();
    }

    var model = new FooViewModel
    {
        Foo = foo,
        Bar = new SomeModelForForm()
    };
    return View(model);
}

If the two things are not related at all, or in particular, if the partial is being called in something like your layout instead of the immediate view, then it's more appropriate to use a child action. Essentially, you'll just handle the display part as if nothing else was going on:

public ActionResult Foo(int id)
{
    var foo = db.Foos.Find(id);
    if (foo == null)
    {
        return new HttpNotFoundResult();
    }

    return View(foo);
}

Then, you'll add another action to handle the form:

[ChildActionOnly]
public ActionResult SomeForm()
{
    var model = new SomeModelForForm();
    return PartialView("_SomeForm", model);
}

Then, add a partial view to render just the form:

Views\Foo\_SomeForm.cshtml

@model SomeModelForForm

<!-- form fields here -->

Then, in your view/layout -- essentially wherever you want the form to actually be displayed:

@Html.Action("SomeForm", "Foo")

Where "Foo" here is the name of the controller this child action is in.

Sign up to request clarification or add additional context in comments.

Comments

0

I recommand you to use model A that contains model B and C. B is loaded on the loading of the page and C is loaded with partial.

One other solution coule bethat you have a model A (given to razor page), then add model B to the page by using partial and then it return a mix of A and B. You just to make attention to the naming of the fields in order to make the model binding work properly, for instance

A has

  • firstName
  • lastName

and B has

  • street
  • phone

Then, if you put edirotfor both fields, model that can be received in controller could be model C that has

  • firstName
  • lastName
  • street
  • phone

Comments

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.