Get the data from the Partial View - asp.net

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

Related

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 : )

ASP.NET MVC3 Partial View naming convention

I'm new to the MVC development so please bear with me. Is it really necessary to name my partial view like _Action.cshtml (with the _ underscore) to comply with the naming convention?
Here's my problem I have a controller (StudentController) and an action (List) that has a partial view file named "List.cshtml", and have
#{ Html.RenderAction("List", "Student"); }
to display this inside my HomeController - Index view as partial view which works. But if I name my partial view to _List.cshtml of course it will not work. Visual Studio could not even find the view for my action Student - List because it think it's still looking for the exact same name as my action (List.cshtml). What should I do?
I m so used to ASP.NET ascx with a pairing ascx.cs code. :(
It's not necessary to use an underscore, but it's a common convention for files which aren't meant to be served directly.
To solve this, you do have the option of returning a View or PartialView with the name of the view as a parameter.
return View("_List");
or
return PartialView("_List");
or inside another view
#{ Html.RenderPartial("_List"); }
If Partial view depends on ActionMethod and always render by Action method, you should same partial view name same as action method like this
public PartialViewResult List()
{
DoSomthing();
//PartialView() return a "List "Parial View
return PartialView();
}
but if your partial view not depends on the action method and directly call by view like this
#Html.RenderPartial("_List");
First, there is no shame to be new to any platform. And this was eight years ago so you are probably not new any more. You can use whatever naming conventions you want to use. I go with the original MVC naming convention which uses underscores (_) only for shared views. Partial views should be named after their actions. In your case the name of the view would be Action.cshtml unless this is a shared view of course.
My reasoning is simple. If you call View or PartialView from an action and don't provide a viewName, it assumes the name of the view is the name of the action. Also _Layout.cshtml is named with an underscore. This is because it is shared, not because it is a partial view. This mistake is all over the place in the MVC world. People are really zealously wrong about it. Don't know why. Naming convention is the shop's discretion.
The helper methods Html.RenderAction and Html.Action call actions on the controller. The helper methods Html.RenderPartial and Html.Partial allow you to pass a model directly to a Razor view without passing through an action.
One final thing, call Action instead of RenderAction. RenderAction is only called if you are already inside of a code block. This is almost never the case. I see people using RenderAction and then adding a code block around it unnecessarily just because the build breaks. These two following code snippets are exactly the same and the second one is way more readable in my opinion. I put the div to emphasize that the code is not in a code block:
<div>
#{ Html.RenderAction("List", "Student"); }
</div>
<div>
#Html.Action("List", "Student")
</div>
The bottom line is don't use underscores or curly braces unnecessarily. They are ugly characters and we should avoid them. ;)

Specify in controller code which partial a view will render

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)

ASP.NET MVC 1, Search and Update data live

Im showing a table of data using a paginated list, with a search box and submit button above this. I wish to be able to search this table of data then re-Post the view and update with the newly searched for data on the click of the submit button. How would I do this in MVC? Would I have to start looking at AJAX or JQuery or can it be done using the built in GET and POST techniques?
Sorry if the question doesn't make a whole load of sense, I'm new here and to MVC :D
The basic pattern is the same whether it's AJAX or not. You would have 2 actions for your view, a GET and a POST, your search form should be a simple ViewModel with the fields you'd search/sort on that also includes your pagination.
[HttpGet]
public ActionResult DataTable(int? page){
var data = myRepository.GetData(page);
return View(data);
}
[HttpPost]
public ActionResult DataTable(int? page, SearchModel search){
var data = myRepository.GetSearchedData(page, search);
return View(data);
}
If you were doing it via AJAX the difference is that you'd have the data display in a PartialView, your DataTable View would render this partial within a named div, the HttpPost method would return a PartialView and you'd replace the named div's contents with this result (JQuery's $.load() method would be the easiest way to do this).

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.

Resources