简体   繁体   English

将值从URL传递到View ASP.NET MVC

[英]Pass value from URL into View ASP.NET MVC

I'm trying to pass the id value of the url into a textboxfor of a view like the picture below,and i have no idea how to do it. 我正在尝试将url的id值传递到如下图所示的文本框中,我不知道该怎么做。 here's the image 这是图像

update, the problem is that value of id is pass from another view by action link like this 更新,问题是id的值是通过这样的操作链接从另一个视图传递的

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

so what i want is to pass an attribute from two different views belong to 2 different controller and different action, how can i do that, i just need to make the attribute from one view appear in other under any type like viewbag or anything. 所以我想要的是从两个不同的视图传递一个属性,这些属性属于两个不同的控制器和不同的动作,我该怎么做,我只需要使一个视图的属性在任何其他类型(如viewbag或其他任何类型)下出现。

when you enter your URL ,you are trying to call a Controller . 输入URL时,您正在尝试调用Controller so you should catch the parameter as argument of the controller method , and then pass it to view with something like viewbag 因此,您应该将参数作为控制器方法的参数,然后将其传递给viewbag之类的视图

in your car controller: 在您的汽车控制器中:

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

   return View();
}

in your view : 在您看来:

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

One simple example in your context- 在您的上下文中的一个简单示例

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

else while returning view, you can do something like- 否则,在返回视图时,您可以执行以下操作:

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

And after that you can- 之后,您可以- 在此处输入图片说明

Textboxfor is binding data with Model. Textboxfor使用Model绑定数据。 So you have to bind view with a model. 因此,您必须将视图与模型绑定。 make AddCart action with id paramter, after that you can see when you pass the value that will appear in id parameter. 使用id参数进行AddCart动作,之后您可以看到何时传递将出现在id参数中的值。 After that load the model or make the model with the id and properties and pass to the view. 之后,加载模型或使用id和properties制作模型并传递到视图。

In the View @Html.TextBox("TextboxName",ViewBag.YourProperty) 在视图中@ Html.TextBox(“ TextboxName”,ViewBag.YourProperty)

In the Controller ViewBag.YourProperty= 'test'; 在控制器中ViewBag.YourProperty ='test';

You should pass the ID through the model defined for this view. 您应该通过为此视图定义的模型传递ID。

Another way is to use Html.TextBox() and provide the value manually using @Request.RequestContext.RouteData.Values["id"] 另一种方法是使用Html.TextBox()并使用@Request.RequestContext.RouteData.Values["id"]手动提供值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从apicontroller传递值到asp.net mvc4中查看 - pass value from apicontroller to view in asp.net mvc4 ASP.NET MVC - 从视图中的 URL 获取参数值 - ASP.NET MVC - Get parameter value from URL in the view 如何在Asp.net MVC中将值从部分视图传递到共享视图_Layout.cshtml? - How to pass value from partial view to shared view _Layout.cshtml in asp.net MVC? 如何在ASP.Net MVC中将类的值从视图传递给控制器 - How to pass a value of class from view to controller in ASP.Net MVC 通过MVC ASP.NET中的超链接将文本框的值从视图传递到控制器 - Pass a value of textbox from view to controller through hyperlink in MVC ASP.NET 单击 ASP.NET MVC 中的按钮时,如何将 model 值从视图传递到 controller - How to pass model value from a view to a controller when clicking on the button in ASP.NET MVC 如何通过Jquery.ajax()将字符串值从视图传递到ASP.NET MVC控制器 - How to pass string value from a view via Jquery.ajax() to ASP.NET MVC controller 如何将隐藏字段值从视图传递到控制器 ASP.NET MVC 5? - How can I pass hidden field value from view to controller ASP.NET MVC 5? 从Controller传递Html字符串以查看ASP.Net MVC - Pass Html String from Controller to View ASP.Net MVC 如何将数据从视图传递到ASP.NET MVC中的UserControl? - How to pass data from view to UserControl in ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM