1

How can I send a string to a partial view?

What I would like is to send information about the model being viewed, to a partial view. Something like this:

@{Html.RenderPartial("_PhaseCreate", new Phase(), @Model.Id );}

Is this possible?

Gonzalo
  • 982
  • 6
  • 23
  • 39
  • Can't you use ViewBag? Set a value from controller or view and access it in partial view. Haven't checked myself yet. – SBirthare Aug 08 '13 at 06:10
  • Possible duplicate http://stackoverflow.com/questions/7177153/mvc3-passing-data-beyond-the-model-to-partial-view. In case there is a particular issue with what you are attempting, share the error or more data about the problem you are facing. There is many post describing how to achieve it normally. – SBirthare Aug 08 '13 at 06:36

1 Answers1

2

If you want to send some data that isn't in model or view, you should use something like the following:

1) instead of @Html.Partial(), use a @Html.Action("ActionName", "Controller", routeValues: new { id = Model.Id }) helper.

2) Add something like this to your controller:

public ActionResult GetMyView(int id)
{
    ViewBag.Phase = new Phase();
    ViewBag.Id = id;
    // also whatever which doesn't in model ...

    return View("_PhaseCreate");
}

And in your partial view, you can use those info just like you declare them:

<label>@ViewBag.Id</label>

You also can simply use the following if you just need to add data existing in model and the view:

@Html.Partial("_PhaseCreate", 
              new ViewDataDictionary(new { Phase = new Phase(), Id = Model.Id }))

and use them like this:

<label>@ViewData["Id"].ToString()</label>
Amin Saqi
  • 18,549
  • 7
  • 50
  • 70