MVC3 RenderPartial and ViewBag

I'll try to keep this text:

In view

@Html.RenderPartial("myview", Model.SubModel, 
     new ViewDataDictionary()
     {
          { "thing", Model.Thing }
     })

      

In myview, we see that the "thing" is available, i.e. this creates the Model.Thing value in the myview:

@ViewBag.thing

      

Fine! Plain

But I need to do something with the thing (and the thing cannot be part of the SubModel by the way), that is, access this in my view module or ideally in a controller, like this:

public ActionResult myview(SubModelType vm)
{
    var thing = ViewBag.thing; // oh dear this doesnt exist.. but is there when the view is rendered

      

So my question is, is the ViewBag available in the resulting myview, should it somehow be passed to the httpcontext or in the controller context somewhere to the right? Does anyone know why it is not available in the controller but in the view and how can I access it?

Edit

Wish I missed a vital point here! This is when "myview" is sent back to the controller by calling an action named myview where I expect the ViewBag to be displayed in myview. But of course not, it was used in "myview" and that's it. So if I want to use it in myview activity, I will need to store in a ViewBag in that view OR set some viewmodel value so that it can be returned to the activity.

It makes sense?

+3


source to share


1 answer


Wish I missed a vital point here! This is when "myview" is sent back to the controller calling an action called myview where I would expect the ViewBag to be exposed to myview.

Oh no, you cannot expect something like this. This is not how ASP.NET MVC works. When you send an HTTP request for a controller action, the binding device intercepts that request by default and looks at the posted values ​​from the request and binds them to your action argument:

[HttpPost]
public ActionResult myview(SubModelType vm, ThingViewModel thing)
{
    ...
}

      

This obviously assumes that these thing meanings were part of the original request. So, for example, if you are submitting a form, you must include the appropriate fields within that form that the binder can use.



Think of it this way: an ASP.NET MVC controller action can be called from any client. For example, from the iPhone app. And, as you know, there is no such thing as ViewBag

. All that will happen is that the binder will look at POSTED values ​​by default and try to moisten the view models that your actions take as arguments.

If, on the other hand, you cannot make these floats part of the request (by including the appropriate input fields in the form), you can only send the id of that subtle from the hidden field, and then inside your controller action, use that ID to query your underlying datastore to retrieve the thing from the same place that you retrieved originally when you first rendered this shape.

Some people may also suggest that you store Thing inside a session, and then read the value back from the session in your POST action. I'm not one of those people. This is an alternative approach.

+1


source







All Articles