2017-07-16 264 views
0

我试图将URL的id值传递到像下面的图片一样的视图的文本框中,并且我不知道如何执行此操作。 here's the image从URL传递值到视图ASP.NET MVC

更新,问题是ID的这个值是从另一种观点认为路过的跳转链接这样

@Html.ActionLink("Chose Car Type for Car", "Addcar", "Car", new { id = item.Ctid }, null)| 

所以我想是从两种不同的观点传递一个属性属于两个不同的控制器和不同的动作,我怎么能做到这一点,我只需要使一个视图的属性出现在任何类型的viewbag或其他任何类型的任何类型。

回答

0

Textboxfor与Model绑定数据。所以你必须将视图与模型绑定。 使用id参数进行AddCart动作,之后您可以看到何时传递将出现在id参数中的值。之后加载模型或使用id和属性创建模型并传递到视图。

+0

所以我应该怎么做,如果我希望把值ID为viewbag代替textboxfor的 –

0

在视图 @ Html.TextBox( “TextboxName”,ViewBag.YourProperty)

在Controller ViewBag.YourProperty = '测试';

0

您应该通过为此视图定义的模型传递ID。

另一种方法是使用Html.TextBox()并提供使用@Request.RequestContext.RouteData.Values["id"]

2

手动当你输入你的网址,你正在试图调用控制器值。所以你应该抓住参数作为控制器方法的参数,然后通过它在你的车控制器查看与类似viewbag

public ActionResult AddCar(int Id) 
{ 
    ViewBag.Id= Id; 

    return View(); 
} 
在你看来

@{int id = ViewBag.Id; }<br /> 
@Html.TextBox("TextboxName",@id) 
0

在您的上下文中的一个简单示例 -

public class HomeController : Controller 
    { 
     public string Index(string id, string name) 
     { 
      return "ID =" + id+"<br /> Name="+name; 
     } 
    } 

else while while returns view,y OU可以做一些喜欢 -

return View("Act", new { yourQueryString= "itsValueHere" }); 

之后,你能够 - enter image description here

相关问题