Why do we use #html.action(string,object)? - asp.net-mvc

I really don't understand the usage of Html.Action(string,object) its return type is HTMl string so why do we even need it? and whats its relation with partialviewresult ? I have seen some people using #html.action(String actionname,Object routeobject); in any of their view and controller invoked by this method returns a partialviewresult whats that ?

Firstly, HtmlString represents an HTML-encoded string that should not be encoded again. HtmlString Class
There is a answer that I believe is good from this question:
#Html.Action and #Html.RenderAction are used when your partial view model are independent from parent model, basically it is used when you want to display any widget type content on page. You must create an action method which returns a partial view result while calling the method from view.
More, use Html.Action when you actually need to retrieve additional data from the server to populate the partial view
Please have a look at the documentation for more detailed information.
More questions about the same topic:
How can I use Html.Action?
MVC Html.Partial or Html.Action
Maybe a bit of research before posting a question would be better

Related

Is returning different view with object bad practice in ASP.net MVC 5?

I need to pass objects between ActionMethods and Views in an ASP.net MVC 5 app.
I'm using it for a multi page signup - and for a multi page payment.
Is this bad practice? I haven't seen a good way to pass objects between different controllers.
Code:
public ActionResult Join1()
{
//
return View("Join2", MyObject);
}
[HttpPost]
public ActionResult Join2(MyObject MyObj)
{
//manipulate object
//return
}
It seems to be an effective way to do it - though I haven't seen many people do it this way. I haven't seen objects being passed between action methods much at all.
Is there a flaw in this approach, a better way of passing models between Views - or should each ActionMethod stick to passing simple data with, say, TempData instead of objects?
Why haven't I seen any sample projects doing things like this?
I've seen return RedirectToAction("act"); plenty - but that is Get and passing an object in a URI is limiting - and I don't want users to be able to manipulate or see the data being passed.
thx.
Unless I have misunderstood the description, your code is not doing what you think it's doing. That first return View("Join2", MyObject); statement is not executing the second ActionMethod, it is only passing your data into the View that happens to have the same name as the second method. Therefore the code implied by //manipulate object will not run before that View is rendered and sent back to the user.
Assuming the View file Join2.cshtml exists, and it contains the default #using (Html.BeginForm()), then users submitting the form will cause the Join2 Action to be executed and the same view rendered with the manipulated data - unless, of course, you add another return View() statement that names a different View.
The reason you haven't seen this done much is that the MVC convention is to have a View named the same as the ActionMethod, this makes the code slightly simpler and also much easier for other ASP.NET developers to understand because it is what they are expecting to see.
If you want the form rendered by each View to then execute a different ActionMethod when it is posted back, the place to do that is in the View code, where Html.BeginForm() has several overloads that allow you to do just that, e.g. in Join.cshtml you could write:
#using (Html.BeginForm("Join2", "JoinController"))
{
// form fields and stuff
}
// Produces the following form element
// <form action="/JoinController/Join2" action="post">
To address the final part of your question, "I don't want users to be able to manipulate or see the data being passed", sorry to say it but your proposed code doesn't prevent that: users can see the data in the web form, before it is ever posted back to the Join2 method; they can manipulate the data by sending an HTTP POST containing any data they want back to the Join2 method.
If you absolutely, positively need to actually execute Join2() from within Join(), before anything is passed back to the user, then you can call it just like any other C# method:
var myResult = Join2(MyObject);
Then you have an ActionResult object that you can manipulate or return straight to the browser. But why you would want to do this, is beyond me.

What is difference between partial and render partial view ?

I am very new in asp.net MVC kindly let me know where is should use partial view and where i should Render Partial view . Thanks in advance
This link might help.
Html.RenderPartial
This method result will be directly written to the HTTP response stream means it used the same TextWriter object as used in the current webpage/template.
This method returns void.
Simple to use and no need to create any action.
RenderPartial method is useful when the displaying data in the partial view is already in the corresponding view model.
For example : In a blog to show comments of an article, we would like to use RenderPartial method since an article information with comments are already populated in the view model.
#{Html.RenderPartial("_Comments");}
This method is faster than Partial method since its result is directly written to the response stream which makes it fast.
Html.Partial
Renders the partial view as an HTML-encoded string.
This method result can be stored in a variable, since it returns string type value.
Simple to use and no need to create any action.
Like RenderPartial method, Partial method is also useful when the displaying data in the partial view is already in the corresponding view model.
For example: In a blog to show comments of an article, you can use Partial method since an article information with comments are already populated in the view model.
#Html.Partial("_Comments")
Both of these helper method are used for rendering partial views
Both have different syntax in razor view #Html.Partial("_student", items) and {Html.RenderPartial("_student", items);}, since render partial return void and the output is written directly to output stream, it has different syntax than Partial.
Syntax in web from view
<%:Html.Partial("_student") %> and <% Html.RenderPartial("_student"); %>
Partial Returns MVCHtmlString, which can be assigned to variables.
Performance wise Render partial is better as it writes directly to output stream.
And of-course you can find a lot of references online and within stack overflow to read

MVC 4 - View That Accepts Multiple Parameters

I have seen a few other threads with the same question as mine but I don't understand them very much. Anyway, my scenario is this: I have a MainView which renders four different PartialViews in it. The reason is because each PartialView contains data that comes from different Tables(Models).
So my problem is this. My MainView accepts a parameter of type string which is an ID. I need to pass that ID to each of my PartialViews. This can be easily achieved but the problem is, each partial views also needs to accept parameter of type List<>.
My question is, how can I pass two parameters to my PartialView (ie string and List<>)?
Currently, my PartialViews accept only one parameter (List<>).
create an object containing both the string and the list<>, and pass that to your PartialViews.
You can use ViewModels. Create a Viewmodel that is the result of all objects you need to present in the view. Is more clean.
There are many articles with this information.

When I call html.renderpartial do I need to specify my model on the first line?

I would like to use renderpartial but I'm not so clear on the way it is used. If I just call render partial then is any model sent to that? I want to use the HTML helpers. Is there any difference between using those in renderpartial and in the main razor view?
If the view actually uses a model and you don't provide any, it just end up having a null Model object (so lots of Null Object Reference Exception).
Just go with
#{ Html.RenderPartial("_partialView", PartialViewModel); }
or:
#Html.Partial("_partialView", PartialViewModel)
The differences between the two are indicated here.
HTH, M.

how to pass data from view to controller in asp.net mvc

What is the best way to pass random data from a view to a controller.
I have a number of views that are identical except for a few pieces of state information. instead of creating a page for each webpage, I want to have one page and I just pass over these variables dynamically.
Although not the "recommended" approach, you can make your action method accept a FormCollection as the parameter. You can then write your own logic to pull whatever data you need from that object. Basically the FormCollection will contain all fields inside the form that is posted as a key-value pair.
The signature would look like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection form)
{
// logic here to
}
Not shure about the "random data" - but maybe the question is realy about "views that are identical"
Put the common parts into a partial and the differing parts into the page, if your layout allows it.
You would have a couple of pages then, but no duplicate code.
Or is your problem more on the controller/model side?

Resources