Tom el Safadi
Tom el Safadi

Reputation: 6776

Asp.net share values across partial views

I am having trouble with sharing the values of my partial views across the views.

My main view:

<ul class="nav nav-tabs" style="margin-bottom: 5%;">
    <li id="first" role="presentation" class="active"><a>Anschrift</a></li>
    <li id="second" role="presentation"><a>Kunden Daten</a></li>
    <li id="third" role="presentation"><a>Preis / Zahlung</a></li>
</ul>

@model CustomerViewModel
<div id="inhalt">
    @Html.Partial("_General", Model)
</div>

The script for my view:

$("#first").click(function () {
    $("#inhalt").load('@Url.Action("General", "Home")');
});

$("#second").click(function () {
    $("#inhalt").load('@Url.Action("Data", "Home")');
});

#first and #second are buttons.

All views are nested inside a controller and my goal is to share the models across the partial views.

My controller:

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

public ActionResult General(CustomerViewModel model)
{
    return PartialView("~/Views/Home/_General.cshtml");
}

public ActionResult Data(CustomerViewModel model)
{
    return PartialView("~/Views/Home/_Data.cshtml");
}

Customer is the main view in which the partial views are getting rendered.

A snippet from my partial view:

@model CustomerViewModel
<h1>General Partial View</h1>
@Html.TextBoxFor(model => model.Name1, new { @class = "text", placeholder = "Name 1", id = "Name1" })

Edit:

I tried the following code in order to share the model with the different views:

$("#inhalt").load('@Url.Action("Data", "Home", model)');

But this didn't seem to work because when I am debugging the controller everything in the model is null.

Explanation:

I press a list element which has an id (#first, #second..). The I want to replace the div with the id of #inhalt with my partial views. In order to do that I am using the javascript to replace the div with the partials, which all works fine. But I am not able to pass my model to the views. How do I achieve this?

Upvotes: 0

Views: 66

Answers (1)

Adam Tuliper
Adam Tuliper

Reputation: 30152

Since you are passing in your model on the route (URI) you'll need to use Route Values here.

<img src="@Url.Action("DisplayData", "Home", Model.RouteValues)" alt="Image" />

As was shown in Binding the Model variable to an Action Method in ASP.NET MVC3

public class MyViewModel
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public bool IsPeriod { get; set; }

    public RouteValueDictionary RouteValues
    {
        get
        {
            var rvd = new RouteValueDictionary();
            rvd["name"] = Name;
            rvd["surname"] = Surname;
            rvd["isPeriod"] = IsPeriod;
            return rvd;
        }
    }
}

or call directly using the Route Values

<img src="@Url.Action("DisplayData", "Home", new RouteValueDictionary(Model)" alt="Image" />

Keep in mind that because you are using the URL you can run across limits in length and the entire model may not fit, in that case you'd need to load via POST and not GET.

Upvotes: 1

Related Questions