views:

65

answers:

3

Hello

I am just playing with ASP.NET MVC 2 for learning. I can pass my models to view but after spend 1-2 hour i am unsuccessful to pass data to view.For example:

www.example.com/home/show/asdf i am trying to get asdf string for show it on screen.

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

With this method i am trying to capture it.Then return view. In my view, i use <%: Html.LabelFor(m=>m as string) %> . This can be looking stupidly. I think that all strings on urls mapping to methods but not integers so i think i have to use question mark like this example.com/home/Test?asdf ? i will try this too.

Edit:

Passing integer on url to method argument get confused me. Example.com/home/test/2 in this url,2 will be argument of test method so i thought same thing for string. I think we can only pass integer and not possible to do samething with any other values.So i think i can only catch values by querystring so still how can i pass a simple string type to view ?

+1  A: 

You cannot call View function like that. Actually you can but it will look for a View named whatever you type in URL. It won't use your Test View. You cannot pass a string as a model because it has to be an object. Check View method overloads. I suggest you create a class for model, then send it to the View.

I suggest something like this.

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

Since you want so badly to pass a string, you should cast it as an object like this

public ActionResult Test(string id)
{
    return View((Object)id);
}
Ufuk Hacıoğulları
By the way how do u catch string on url ? example.com/home/test/asdf doesn't catch anythinhg when i test it.
Freshblood
You can (but it usually isn't the best idea as you probably need more data anyway). You just cast it to object and the right overload will be called: return View((object)id);
Mattias Jakobsson
When i test it i see id is null
Freshblood
Is there another route in global.asax that you defined? It should work with default route.
Ufuk Hacıoğulları
routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "index",id=UrlParameter.Optional}this is my global.asax and there is no another one
Freshblood
I cannot see what's wrong here. You can catch it as string in URL, no doubt there. What's your controller and method names and what URL do you use to call them?
Ufuk Hacıoğulları
example.com/home/test/asdf i wrote such a url and i see that my method works but argument is null.
Freshblood
I'm lost, I don't have any idea why that would happen. What makes you sure it's null, where do you display the value?
Ufuk Hacıoğulları
on mouse hover in debug time it is written null.I got null exception when i tried access string methods. I created new mvc project and result is same. Do u catch string in url with your string parameter ? Which MVC version do you use ?
Freshblood
Yes I catch the string ofc. I use MVC-2 though I did not update it recently. Do you use "id" for the name of parameter? I tried to give it another name and it got null value.
Ufuk Hacıoğulları
what change if change parameter name ?
Freshblood
The parameter gets null value if I name it other than "id" or "ID". I think that happens because it's defined in default route. If you use some other name, you must call it with querystring.
Ufuk Hacıoğulları
Huhhh i really get suprised about it.It is strange behavior really
Freshblood
It is always null on me. Let's more effort to solve it :D i will research about it . I am new on MVC pattern and implementation of MVC pattern was really hard enough at beginning but still i see that i will need time to understant it is strange structure and behavior.
Freshblood
A: 

Try

public ActionResult Test(string ID)
{
 return View("Test", ID);
}
JonoW
Actually it won't work. There's an overload of View method that takes two string parameters. It won't pass ID as model
Ufuk Hacıoğulları
Ah yes good point, forgot about the master name...
JonoW
+1  A: 
public ActionResult Test(string ID)
{
 ViewData["ID"] = ID
 return View();
}



<p>


<div class="simple">
<%= Html.Encode(ViewData["ID"]) %>
</div>
volkan er