ASP.Net MVC Reading Action Parameter/ Route Info in a RenderPartial - asp.net-mvc

In my MVC app I have one action available on a public side so everything looks like http://www.domain.com/pagename
When it goes to the action method of my WebPageController it finds it in the DB by pagename and renders the View.
In the View I do a database call to a foreign key table called WebPageContent. For each WebPageContent I do a RenderPartial(WebPageContentItem.ControlType)
This works for the most part, I now have a need to have http://www.domain.com/pagename/idofsomething
I have made the changes relevant and the Controller is firing it. What I want to do is if the ID is passed to the Controller I need to get it to my RenderPartial call. I have got it working by setting the ID via ViewData but didn't know if that was the best approach?
Controller->ViewData->View->RenderPartial("Name",Model,ViewData)
In the actual UserControl can you read the action parameter or get it from ViewContext or similar?
In my RenderPartial I can then make a DB call to find the relevant item by the passed ID and then render relevant HTML from the DB object.

<% var id = ViewContext.RouteData.Values["Id"]; %>
SO - Getting the name of the controller and action method in the view in ASP.Net MVC ...

Related

Html.BeginForm does not redirect to action Method

I try using Html.BeginForm to submit my data but it does not help me with it. I followed this link to make my mvc program: https://www.c-sharpcorner.com/UploadFile/f82e9a/form-data-submiting-and-displaying-data-another-view-using-m/
#using (Html.BeginForm("SubmitEmp","ClientController",FormMethod.Post))
using this I get the following error:
My view has a folder named Client, in which there are two files:
Index: this contains the form.
this the view to get the details entered in the previous form.
My controller (ClientController) has the code mentioned in the link
and my Model is Cmodel with the same code.
Your Code should be like this then it will work
#using (Html.BeginForm("SubmitEmp","Client",FormMethod.Post))
We cannot and use full controller name like ClientController for specifying controller name we need to specify only the name of Client
In your URL localhost:5000/ClientController/Submitemp showing controller name ClientController it should be Client only change your URL like localhost:5000/Client/Submitemp then it will work.
Cheers!!
No need to pass "Controller" in controller name, simply pass "Client". Make sure you have "SubmitEmp" HttpPost action in Client Controller
I can see that you have passed controller name as 'ClientController' which appears to be incorrect. The Controller in 'ClientController' represents that this is a Controller named Client. You can pass 'Client' instead of 'ClientController' in the place for controller.

MVC Create method data flow

I think i know some of the basics of MVC but there's one thing which I don't understand yet.
In the view Create which is generated automatically when you set up your project, how is data sent to the controller? I'm used to seeing ActionLinks with parameters but here there's no actionLink so I can't understand how data travel from the view to the controller.
Could you explain it to me please?
as you know, in your view, the very first line (usually) tells the view, about the Model being used within this view. like:
#model Models.CarViewModel
lets suppose, you have a form on this view, and it is posted to some action called Edit. Then you must have your Edit action, expecting the parameter of type you used as model in your view. like:
[HttpPost]
public ActionResult(CarViewModel model)
{
//logic
}
This convention is known as Strongly Typed View. Suppose Then you have some textbox for a property Name of your model as:
#Html.TextBoxFor(x => x.Name)
when the form is posted to Edit Action, the variable model in parameter of Edit action will be holding the respective values. i.e, model.Name

I have a ViewModel strongly bound to my View in my ASP.NET MVC app, now what about the controller

So like the title says, I created a view model in my asp.net mvc application so it would be strongly typed to my view. My view model is a combination of two of my model classes. Now when the user hits the save button on that view, it goes to a controller. How does it know what controller to go to? I built my controller 1 - 1 so to speak with my models and views so controller A knows about Model A and controller B knows about Model B. But if I have a view model that is AB how does it know on subit to go to A or B. Do I need a controller called AB Controller?
The controller and action invoked do not depend on the viewmodel object you used to bind the page during initial view rendering. The controller (and action) invoked is determined by the URL of the request sent. (One of the routes you have defined will be matched based on the URL string of the request or a 404 not found error will be returned.)
A submit button in an HTML form (usually a POST) will have an action attribute that determines its url target, an anchor tag will have an href, etc.
In your situation where you have a custom viewmodel object you can define an action to expect and to attempt to parse that specific type of object by specifying it as the parameter to your action:
public ActionResult SaveSystemSetting(SystemAdminVM item) {
An Action Method doesn't receive a model. It receives parameters.
Anyway I think I know where you come from: One of the parameters of your action has the type of the ViewModel used in the View. That is a common layout and makes a lot of sense. In lots of projects it is so widely used that after some time you start to think that an action actually receives a model.
Just remenber that the parameters of your action can by anything that can by filled by the ModelBinder. You could pass both Models as parameters, you could pass a ViewModel that agregates Model a and b or you could pass a ViewModel that has properties of a + b.
Agregation ist the most common approach.
Generally we overload our action methods in the controller so if you have an action called edit that renders the view with your viewmodel object, you will have an overloaded edit action method with the HttpPost specified for that method.
The data to this method will be passed as form value collection values or you can bind it to your viewmodel object if you like and process it further.

MVC actionlink posting List of complex type

I have an actionlink that on click im passing a List of objects to a controller action.
Example:
View:
Html.ActionLink("TestLink", "TestMethod", "Test", Model.SampleList, null)
TestController:
public ActionResult TestMethod(List<SampleList> sampleList)
{
return View(sampleList);
}
When I do this I get a null sampleList. I can pass a single complex object fine just not a collection of it. Do I need the correct routing for this? The reason I'm doing this is instead of passing an id and do a look up in the controller action, I just pass in the data.
It is possible when you perform a form post, have a look at this blog post for more information. You'll probably not be able to use one of the HtmlHelper methods though, the post states:
Currently, we don’t have any helpers
for generating the form, so this is a
very manual process.
Nothing prevents you from writing your own helper though.

MVC 2 RenderAction

Im having a value in ViewData, lets say htmlhelper.ViewData["myData"]="some";
And in partial page I can overwrite the myData's value.
But when I using the Html.RenderAction() and call a partial page.
In the same partial page htmlhelper.ViewData["myData"] is null.
When you call RenderAction, you create an entirely new ViewData instance for your partial page. If you want ViewData["myData"] to be visible by your other action, either pass it to the subaction or put it in TempData.
I figured out from MVC source code. Cool that we have MVC as open source.
htmlHelper.ViewContext.HttpContext.Items["myData"]
this will maintain the value from Partial and RenderAction case.

Resources