I can not pass the value to view

I just play with ASP.NET MVC 2 for training. I can transfer my models for viewing, but after spending 1-2 hours I was unsuccessful in transferring data for viewing. For instance:

www.example.com/home/show/asdf I'm trying to get the asdf string to show it on the screen.

public ActionResult Test(string ID) { return View(ID); } 

With this method, I am trying to capture it. Then return the view. In my opinion, I am using <%: Html.LabelFor (m => m as a string)%>. It might look stupid. I think all the strings are for matching URLs to methods, but not integers, so I think I need to use a question mark like example.com/home/Test?asdf? I will try to do it too.

Edit:

Passing an integer value by url to a method argument confuses me. Example.com/home/test/2 in this url, 2 will be the argument of the testing method, so I thought the same for the string. I think that we can only pass an integer and cannot do something with any other values. So I think I can only catch the values ​​with querystring, so how can I pass a simple string type to look at?

0
source share
3 answers

You cannot call a view function like this. You can actually, but it will look for the View that you specify in the URL. It will not use your test view. You cannot pass a string as a model because it must be an object. Recheck the verification method. I suggest you create a class for the model and then send it to the view.

I suggest something like this.

 public ActionResult Test(string id) { SomeModelClass model=new SomeModelClass(id); return View(model); } 

If you want to pass a string, you should use it as an object like this

 public ActionResult Test(string id) { return View((Object)id); } 
+3
source
 public ActionResult Test(string ID) { ViewData["ID"] = ID return View(); } <p> <div class="simple"> <%= Html.Encode(ViewData["ID"]) %> </div> 
+1
source

Try

 public ActionResult Test(string ID) { return View("Test", ID); } 
0
source

All Articles