0

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. here's the image

update, the problem is that value of id is pass from another view by action link like this

@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.

5 Answers 5

2

when you enter your URL ,you are trying to call a Controller . so you should catch the parameter as argument of the controller method , and then pass it to view with something like 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)
Sign up to request clarification or add additional context in comments.

Comments

1

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- enter image description here

Comments

0

Textboxfor is binding data with 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. After that load the model or make the model with the id and properties and pass to the view.

1 Comment

so what should i do if i want to put that id value into a viewbag instead of the textboxfor
0

In the View @Html.TextBox("TextboxName",ViewBag.YourProperty)

In the Controller ViewBag.YourProperty= 'test';

Comments

0

You should pass the ID through the model defined for this view.

Another way is to use Html.TextBox() and provide the value manually using @Request.RequestContext.RouteData.Values["id"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.