ASP.NET MVC 2 Submitting from partials in different views - asp.net-mvc

If I have a partial that is used in multiple views that submits to its own dedicated action, then how do I know which view it has come from so I can go back to that view?

I think it's answered here: Retrieve the current view name in ASP.NET MVC?

Just to clarify - How do you mean 'so you can go back to that view'
If you use ajax forms ie Ajax.BeginForm for each of those partial views, they will all automatically handle their own psots to the url (hence controller) and can live peacefully in the parent view no matter which view it is.
if model validation happens, then you will see it in those partial views and modelstate will repopulate the posted values into the model for the user to 'fix'

Related

Using MVC Partial View's own model instead of its parent's

I need to populate a list box in a partial view, using ASP.NET MVC4.
Can Partial View have its own #model, as opposed to taking a model from its parent as described here?
I can populate my dropdown box using a separate AJAX call to another MVC controller (i.e. not parent page/url) as discussed here, but the resulting syntax is a bit more clumsy; furthermore, an additional endpoint is exposed to the outside world.
Yes - you can call partial view with any model you like. There is no requirement that data somehow comes from current model.
#Html.Partial("PartialView1", new MyOtherModel(42))

How do I submit a form within a partial view?

I am asking this question to make sure myself that I understood the subject.
"The only way to have a submit button inside a partial view is using Ajax techniques to submit the form"
Well, technically you could do a normal postback from a partial view. But that would tend to defeat the point of a partial:
the entire page would have to be re-loaded, which means the result of the partial-view postback would need to be a complete page.
the partial view's modularity suffers, because the result of its postback falls out of the partial view's scope (ie, the partial view needs to know about the entire current page).
So, it's an irregular thing to do a non-AJAX postback from a partial view. But there are scenarios where it would be useful/appropriate. Consider for example a "login" partial: you may want to post the credentials back to a specific controller/action, and have that action redirect back to the current page. In that case you could reasonably use a non-AJAX form.
The only way to have a submit button inside a partial view is using Ajax techniques to submit the form
You didn't say where you got the quote from, but this isn't true.
You submit a form from a partial view in exactly the same way you submit a form within a main view. You can do this using custom fields or by making your partial view strongly typed. Where are you getting hung up?

Get partial value control value in controller action

I m working on asp.net mvc application.
i have one partial view in that one submit form and click on submit button than data will stored in database.
but when i get data from form collection than that form collection come null so how can i get partial view control's value in action method?
thanks in advance..
note : partial view is not strongly type.
I think you encounter the same issue that is discussed here:
ASP.NET MVC partial views: input name prefixes
To quickly investigate the issue, look at the source code of your page: do your controls from partials have the names you expect? If not, the article above will be helpful.

ASP.Net MVC reusable form as RenderAction or RenderPartial

I'm looking for a best practice for embedding a form on multiple pages as a partial view.
I have a contact form I'm looking to embed on multiple pages on a site. Usually, the form would be on a contact page and the contact model could be the model for the view and use data annotations for validation. However, the view is already strongly typed.
How can I create a reusable form in a partial view and embed it on the page? I'm using N2 on the site, so the pages have to already have a strongly-typed model, but I would be open to extending those objects.
Personally, I recommend using for Html.RenderAction() for cross-cutting concerns such as these.
The handler for your contact form is going to need to exist independently of the page your are currently viewing so you are left with 3 options:
Manually add it to the response of
the current action
Manually add it to the response of
the current controller by way of a
base controller that modifies the
ViewState or ViewModel
Call the RenderAction()
HtmlHelper inside of the current view
Of these 3 options, while the third is technically more costly than 1 and 2 (because it initiates a brand new request), it is also the most maintanaible solution. By calling RenderAction() you have the advantage of being able to completely isolate your contact form from the rest of the view and thus you won't have to worry about hacking it into the current controller responses.
Use RenderPartial if data model for partial view is already in main view's model, in other case use RenderAction (then the action of the partial view will create its view model itself).

Pass data to Master Page with ASP.NET MVC

I have a hybrid ASP.NET WebForms/MVC project. In my Master Page, I have a "menu" User Control and a "footer" User Control. Anyways. I need to pass some data (2 strings) to my "menu" User Control on my Master Page (to select the current tab in my menu navigation, etc.) My views are strongly-typed to my data model. How can I push data from my controller to my menu or at least allow my master page to access some data pre-defined in my controller?
Note: I understand this violates pure ASP.NET MVC, but like I said, it is a hybrid project. The main purpose of my introduction to ASP.NET MVC into my project was to have more control over my UI for certain situations only.
Put your strings into the ViewData collection,
ViewData["MenuString1"] = "My First String";
ViewData["MenuString2"] = "My Second String";
and retrieve them in the Master Page like this:
myMenu.Property1 = ViewData["MenuString1"].ToString();
myMenu.Property2 = ViewData["MenuString2"].ToString();
http://nerddinnerbook.s3.amazonaws.com/Part6.htm
You can use ViewData to share data between the Controller and View that is not in the model. In the Controller, do something like ViewData["menu"] = myMenu; and then in the View do <%= ViewData["menu"] %>. You can pass objects but you need to cast the ViewData[key] back to the object type in the View.
Another way to do this is to put your menus and other non-Model related data needs into a separate controller. Then you can use RenderAction to call the action in your navigation controller that generates the menu. Hack has a blog post on RenderAction that explains this in more detail.
I lean towards using ViewData for temporary values from the controller like select lists and a RenderAction for unrelated things such as the main site navigation.

Resources