我不能传递一个值来查看 [英] I can't pass a value to view
问题描述
我只是用ASP.NET MVC 2玩中学习。我可以通过我的模型来查看,但花费1-2小时,我是不成功的将数据传递给view.For示例后:
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我试图得到它显示在屏幕上ASDF字符串。
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);
}
通过这种方法,我试图捕捉it.Then返回查看。
在我看来,我使用<%:Html.LabelFor(M =>米的字符串)%>。这愚蠢的寻找。我认为对的URL映射所有字符串的方法,但不是整数,所以我想我必须使用问号这样example.com/home/Test?asdf?我会尝试这一点。
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.
编辑:
上的URL方法参数传递整数感到困惑了我。 Example.com/home/test/2在这个网址,2将是测试方法的参数,所以我想同样的事情串。我认为,我们只能传递整型和不可能做samething与任何其他values.So我想我只能通过查询字符串所以还是我如何可以通过一个简单的字符串类型,以查看捉值?
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 ?
推荐答案
您不能调用像取景功能。其实你可以,但它会寻找一个名为视图无论你在输入URL。它不会将您的测试视图。你不能传递一个字符串作为一个模型,因为它必须是一个对象。检查查看方法重载。我建议你创建模型类,然后将其发送到视图。
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.
我建议这样的事情。
public ActionResult Test(string id)
{
SomeModelClass model=new SomeModelClass(id);
return View(model);
}
如果你想传递一个字符串,你应该将它转换为这样一个对象
If you want to pass a string, you should cast it as an object like this
public ActionResult Test(string id)
{
return View((Object)id);
}
这篇关于我不能传递一个值来查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!