asp.net mvc - Equivalent of html.actionlink to be called in the controller? -


I'm setting up my title tag controller:

  ViewData [ "H1" ] = "Home", "+ category +" & gt; "+ Subcategory;  

I want Home and category to be the URL, what is the best way to go about doing this is?

you should not do that. I ask every question that controller from HTML to be made. Controllers should not make HTML, this is a view for which instead, pass to see the category and subcategory, and generate HTML there. Not:

  public action Foo () {ViewData [ "category"] = "Foo"; Dekendeta [ "subcategory"] = "bar"; return View ();} & Lt; ASP : content ID = "content 1" content Plesholderaidi = "main content placeholder" Rnat = "server" & gt; & Lt; h3 & gt; Home & gt; & Lt;% = Html. Akshnlink (Dekendeta [ "class" ])% & gt; & Amp; gt; & Lt;% = Html.ActionLink (Dekendeta [ "subcategory"])% & gt; & lt; / h3 & gt;  

Of course, to use a strongly typed model rather than the View Dat dictionary, The cross, but it should give you a general idea.

Note: My basic answer is as follows. Basically the question (see revision) was completely different from the question because now it stands. I am leaving the original answer, because I think there is some value in it.

Before I answer, I would remember if I did not say that this example is wrong in some different ways.

  1. You should use the code behind it in any MVC app, at least in debacable.
  2. You should not have to set ViewData in a View ViewData should be set in the controller and read in a view.

Now, he has said, how to make the HALL call. The code behind the Actionslink page is in the code, um, just call it. It works just fine, returns a string, as it happens in ASPX.

However, it is not what I recommend doing. Instead, make a helper for your header:

  1. Create a new, stable class. Check it out helper or some call.
  2. Web. Add the namespace of this class to the Page-> Namespace section in the config
  3. Add a new method, header (or the name you like) towards the class. See the examples below.
  4. Your view is now called helper. Again, see the examples given below. Examples:

      public stable class ViewHelpers {public static string header (this HtmlHelper helper, string category, string subcategory) {return string (format " Home & G; {0}> gt; {1} ", helpful. Akshnlink (category), subsidiary. Akshnlink (subcategory);}} & asp: Content ID =" Content1 "ContentPlaceHolderID =" MainContentPlaceHolder "Runat =" server " & Gt; & lt;% = Html.Header ("foo", "bar")%> 
    [[...] & gt; [...] & lt; Namespace & gt; [. ..] & lt; add namespace = "your namespace.contenture class" /> gt; & lt; / name Spaces & gt; & lt; / pages & gt;

Comments