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.
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)
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. 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.
In the View
#Html.TextBox("TextboxName",ViewBag.YourProperty)
In the Controller
ViewBag.YourProperty= 'test';
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"]
Related
So my mvc controller method here take an int id as an argument. Im wondering why new "{ id = restaurant.Id}" is required and "restaurant.Id" is not sufficient.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Restaurant restaurant)
{
if (ModelState.IsValid)
{
db.Update(restaurant);
return RedirectToAction("Details", new { id = restaurant.Id });
}
return View(restaurant);
}
That is because MVC expects an object from which to retrieve the values to bind to the action parameters. In other words, you could do:
return RedirectToAction("Details", restaurant);
Mvc will crawl your object for properties that match the name and type of your action's arguments and should therefore bind to the restaurant's 'Id' property.
If you just pass the restaurant's Id, though, MVC will crawl the type of whatever you restaurant's id property is.
Hope this helps. If it does, please mark it as the answer ;)
I've got a certain action in my controller.
I've created a view for it and linked a model in it... now I want to pass some info from a property from this model onto another action safely... Only ways I know are to pass it with #Html.ActionLink /with a hidden field in a form.
But these aren't secure at all as far as I know... so what other way is there to do this ?
You can store it in a session variable:
To store a property of the model in the first action:
public ViewActionResult SomeAction(SomeModel model)
{
Session["remember"] = model.someProperty;
return View();
}
To retrieve it in another action:
public ViewResult SomeOtherAction()
{
var rememberedValue = Session["remember"];
return View();
}
I'm developing a Web Application by using ASP.Net MVC 5. My model is something similar to:
Person
- int ID
- string FullName
- int PersonTypeId
PersonType
- Id
- Name
- Description
I'm working on the "create new Person" page. I have created a ViewModel with the following structure:
public class SampleAddViewModel
{
public Person person;
public SelectList personTypes; // Used to populate the DropDown element.
}
My controller's GET method (to simply display the page):
// GET: Add new person
public ActionResult Add()
{
SampleAddViewModel savm = new SampleAddViewModel();
AddPersonViewModel.personTypes = new SelectList(PersonTypesEntity.GetAll(), "Id", "Name");
return View(savm);
}
In my controller's POST method (to store the created person) I would expect to just receive the Person model, and not the entire ViewModel. But on the View page I think it is only possible to declare an #model razon line, which I think it must be #model SampleAddViewModel ...
Would it be possible to, in the POST Add entry, have something similar to the following:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Add([Bind(Include = "ID, Name, PersonTypeId")] Person person)
{
if (ModelState.IsValid)
{
db.Add(person);
db.SaveChanges();
return RedirectToAction("Index");
}
x <- //Should I re-create the ViewModel in here?
return View(x);
}
Which would be the best way to address the problem? I'm also trying to avoid using ViewBag. Maybe the best way in fact is to re-send the entire ViewModel.
If you have any errors on the server side on the POST method then yes, you'd want to send the ViewModel back to the view.
Since you are only sending a Person instance into the controller action and your View is expecting an instance of SampleAddViewModel, you should create an instance of one of these and pass it to the View; After all, you're going to need to repopulate the personTypes dropdown with data again.
I have models (POCO entities) like Student, Course, Standard etc. I have corresponding controllers such as StudentController etc. I have a view Index for each model which displays the list of all the corresponding entities in DB. For example, StudentController.Index() returns the /Student/Index view. However, if there are no Student records in the DB, instead of returning the Index view , I redirect to the Empty action method of the Navigation controller, i.e. NavigationController.Empty(), which returns the /Navigation/Empty view. This is done for all model entity classes.
Now, on the empty page, I wish to have a hyperlink to go back to the previous page. So I created an action method called GoBack() in the NavigationController class, in which I redirect to the previous view. But how can I access the information about what the previous page was in this action method? Or is there a better way to do this? I do not want to use the back button.
As far as I'm concerned there are a couple of routes to take here. You could use sessions or the application cache to store a las visited page, and then get that page (by storing a route for instance) in the GoBack() action using a RedirectToAction.
But maybe a nicer and stateless aproach would be to render the hyperlink by having a view model having two properties for last used controller & action. Then you could pass these from the action result calling the /Navigation/Empty action (when there aren't any records).
ViewModel
public class NavigationVM
{
public string LastAction {get;set;}
public string LastController {get;set;}
}
Navigation Controller Action
public ActionResult Empty(string lastAction, string lastController)
{
var vm = new NavigationVM()
{
LastAction = lastAction,
LastController = lastController
}
return View(vm);
}
View
#model = Namespace.NavigationVM
#Html.ActionLink("LinkName", Model.LastAction, Model.LastController)
EDIT
If you then need to find out from where the students controller was called (in your example) you can go about this the same way. I.e.: Render the link to the StudentsController with extra route values.
StudentController:
public ActionResult Index(string lastAction, string lastController)
{
.... // no students
return RedirectToAction("Empty", "Navigation", new RouteValueDictionary(new { lastAction = "Index", lastController= "Student"}));
}
View with hyperlink to students controller (use the action and controller that rendered this view as lastAction and lastController respectively):
#Html.ActionLink("Get students", "Index", "Student", new { lastAction= "Index", lastController = "CallingController" }, null)
I have a hidden field in my HTML view
<%:Html.HiddenFor(model=>model.ContactId) %>
I want to put this value in viewBag so that I can use it in controller. How to do that? Also, how will I access this in controller?
You don't have the notion of postback in ASP.NET MVC and the ViewBag is available to you to deliver data to the view, but not to transfer data back and forth between the view and the controller.
If your hidden input is in a form then upon posting the form the new value would be accessible via model.ContactId.
Example:
// Model
class TestModel
{
public string ContactId { get; set; }
}
//Controller
[HttpPost]
public ActionResult Edit(TestModel model)
{
string newId = model.ContactId;
}