开发者

.NET MVC3 + Razor Multiple RenderView/RenderAction

My partner and I decided to create an app in .NET MVC3 using Razor, since neither of us have used either .NET MVC or Razor before. I have a page that has a 'user dashboard' to which I've added several RenderView/RenderAction sections, usually with forms. All of these forms are converted to jQuery JSON forms on load:

 $('#main').delegate("form:has(.siteFormField)", "submit", function () {/*form and submit logic here*/})

and receive back partial v开发者_JAVA技巧iews which are usually updated versions of the forms along with a status message.

I have one set of forms where the input on one effects the contents of another (hit a 'register' button and the item is moved to the other list). I'm rendering both forms in a single partial view which is included in the dashboard using a RenderAction to call a controller, which returns the view with both forms updated. The form with the buttons, however, calls a different controller, which usually returns just the updated form and a status message. Usually I would do all this in JavaScript, calling methods and parsing the result to determine how to transform the DOM, but we're trying to make this as MVC+Razor as possible. Is there a clean way to POST a model to a controller and have the result return a different view based on submission context, or am I over thinking this problem?


The cleanest way is to create one action/model/view for each of your scenarios. You can have one action return a different view/model. The view knows where it needs to post the model it supports.

Of course you can do more complicated things, like build your model as a javascript object and post JSON to an action.


Yes, there are several ways to accomplish this. Two straighforward ones:

  1. Have your initial method a Redirect to the appropriate controller method based on the request contents. If your parameters are all passed via GET, this is a quick and easy way to go. The downside is that POST data is not redirected, so it you're calling an [HttpPost] method it won't work.

  2. The View method on the MVC Controller base class takes a lot of overloads. One of them takes a view name, allowing you to do something like this:

    switch ( foo ) 
    {
        case 1:
            return this.View("View1", InputModel.Case1Data);
        case 2:
            return this.View("View2", InputModel.Case2Data);
        default:
            return this.View(InputModel.FallbackData);
    }
    

EDIT: I think I may have not fully understood your question so, to clarify:

You would need to come up with a single controller method that can act as the "dispatch" method, and all of your forms post to that. The method, internally, can return different data based on the model data you pass in.

For working up the model, you have some options there as well:

  1. Make one big model with the union of every field on each individual model, and fill in what you need.

  2. Make a heirarchical model with the individual models as child properties of a parent container. MVC is very good with turning child objects (and arrays) into bound fields, and back again. So an HTML input with a name like "Case1Data.Name" would get bound into the Name field of the Case1Data field of the model class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜