Specify in controller code which partial a view will render - asp.net

I have a RegisterView, used for three different actions. Until now it was only used for two, and I have a FormVisible flag on my view model that the controller sets for the first action, which uses the visible form to collect user details. On the second action, confirming the registration, the details form is not visible.
I now have two sets of details to collect, so instead of a boolean decision of whether on partial view must be rendered or not, I need a way for the controller to specify which partial view to render. How can I do this?

have not tried this, but you can pass the name of the partial view in either the view model or the ViewBag, then use that in your view to render the partialview
in the controller:
ViewBag.PartialView = "PartialViewA";
in the view:
#Html.RenderPartial(ViewBag.PartialView)

Related

Get the data from the Partial View

I have a partial view that is strongly typed. I have a drop down inside that partial view. I have rendered that partial view on the view. I need to get the item that was selected on the dropdown on the form submit. How do I get it?
Any help is appreciated.
Thanks,
Naveen
Give that drop down a name attribute at render time. Then, in post Controller Action, make sure to have a string with the same name as a parameter. Binding is done automagically for you by the framework
For example, in your view, you'd have something like this:
Html.DropDownList("myDropDown", SomeListOfValues);
Then in your controller's post action:
public ActionResult MyPostAction(string MyDropDown) {
// do whatevs with it
}
P.S. same idea works even if you construct your list using just HTML. Set some breakpoints and try it out for yourself

How do you send a controller action to a partial view in lieu of a model?

So I'm trying to add a partial view to my main view in MVC3, but the partial view needs new data. Instead of expanding the view model that has the necessary data in the main view and then passing it along to the partial view, is it possible to specify a controller action that directly feeds the partial view with the necessary model?
For example something like:
#Html.Partial("_PartialView", Controller, Action, Parameters)
Thanks in advance.
In a limited sense, yes.
The only thing you can do is send the current model over to another action through Html.Action
Besides that you either need to add it to TempData, or pass what's required in the querystring through your GET parameters OR use an ajax request where you write these values to an html form and serialize that to your new page, but thats a hack : )

Action called twice

I have a #Html.Action() in my layout, and putting a breakpoint in the controller action behind this shows it is being called twice (one seems to be as part of the overall controller action returning my main View and the second seems to be on the #Renderbody() call).
This results in my partial view being returned from the action (depending on the user role) being shown twice, once in the right place in the layout (where the #Html.Action() call is and once within the rest of the page, just before where next #Html.Action() call is inside the main page being shown in the layout.
I assume it has something to do with #Renderbody() displaying all partial views returned from the controller but I have no idea.
Any pointers on if this is true, and if so how can I show my menu without using an action?
I had a similar situation where I was calling an ActionResult using #Html.Action in order to render a partial view (after processing some data), however it kept repeating the layout twice. To fix it I had to change the ActionResult to a PartialViewResult, even though both were returning a partial view, the former seemed to treat it as if it were returning a view anyway...

Asp.Net MVC Not Duplicate forms when Edit/Add

When we have anything that requires user input (Eg adding a product to a database) the Edit screen looks the same as the add screen. When using MVC .Net how do you handle this? Do you return the same view? Just adjust the model to reflect the change?
Same (partial)view
You create only one but strong typed view (depending on the UI it can of course be a partial view as well). When adding new data return this view from controller action with default model object instance (usually just a new instance without any properties being set), but when you edit, return it with the object instance that you'd like to edit.
Controller part
Regarding controller actions you can have four of them:
Add GET
return View("SomeView", new Customer());
Add POST
Edit GET
return View("SomeView", new CustomerRepository().GetCustomer(id));
Edit POST
Bot GET actions return the same view but with different model as described earlier. POST actions both store submitted data, but return whatever they need to. Probably some RedirectToAction()...
You can use the same view for display and Edit, simply call it from your controller
return View("ViewName")
You could have the form fields in a partial view and have two separate views using the same partial view, one posting to the edit controller action method and the other posting to the add controller action method.
Partial views are used to remove duplicity. You could read an example of this in the Nerd Dinner tutorial.

ASP MVC: Submitting a form with nested user controls

I'm fairly new to ASP MVC so go easy :).
I have a form that contains a number of user controls (partial views, as in System.Web.Mvc.ViewUserControl), each with their own view models, and some of those user controls have nested user controls within them. I intended to reuse these user controls so I built up the form using a hierarchy in this way and pass the form a parent view model that contains all the user controls' view models within it.
For example:
Parent Page (with form and ParentViewModel)
-->ChildControl1 (uses ViewModel1 which is passed from ParentViewModel.ViewModel1 property)
-->ChildControl2 (uses ViewModel2 which is passed from ParentViewModel.ViewModel2 property)
-->ChildControl3 (uses ViewModel3 which is passed from ViewModel2.ViewModel3 property)
I hope this makes sense...
My question is how do I retrieve the view data when the form is submitted? It seems the view data cannot bind to the ParentViewModel:
public string Save(ParentViewModel viewData)...
as viewData.ViewModel1 and viewData.ViewModel2 are always null. Is there a way I can perform a custom binding?
Ultimately I need the form to be able to cope with a dynamic number of user controls and perform an asynchronous submission without postback. I'll cross those bridges when I come to them but I mention it now so any answer won't preclude this functionality.
Many thanks.
For databinding to work you need to give special names to your text fields. For example in ChildControl1:
<%= Html.TextBox("ViewModel1.Property1", Model.Property1) %>
This will generate the following markup:
<input type="text" name="ViewModel1.Property1" id="ViewModel1_Property1" value="" />
And when you post to the following action:
public ActionResult Save(ParentViewModel viewData)
{
...
}
It will update the value of viewData.ViewData1.Property1. For binding collections you may take a look at this article.

Resources