I need to pass a view data(integer) to another controller.
This is what i tried;
#Html.ActionLink("Get Location", "Index", "Map", new { Id=#item.Id},null)
i need to pass this information to "Map" Controller's Index action method;
public ActionResult Index(int? i)
{
var Id = from o in db.Objects where o.Id == i select o;
return View(Id);
}
but the parameter doesn't get pass...is this the way to pass the parameter??When i put a break point i found that int? i is null..why is that??
The parameter you're passing is Id, but your parameter in your action is i.
Rename i to Id.
Html.ActionLink("Get Location", "Index", "Map", new { id=#item.Id},null)
public ActionResult Index(int id)
{
var Id = from o in db.Objects where o.Id == id select o;
return View(Id);
}
Related
In this actionLink I get the id from Cliente.
#Html.ActionLink("Pets", "Create", "Pets", new {id = Model.ClientId}, null)
Than i send this id to a view bag in PetController
public ActionResult Create(int id)
{
ViewBag.ClienteId = new SelectList(db.Clientes, "ClienteId", "Nome");
return View();
}
But when a run the code, the dropdown show more than one Client.
As you can see in this image.
how can i show only de client who have the id that a i get in the actionlink ?
Thank's.
At Html
#Html.ActionLink("Pets", "Create", "Pets", new {id = Model.ClientId}, null)
At PetController
public ActionResult Create(int id)
{
ViewBag.ClienteId = new SelectList(db.Clientes, "ClienteId", "Nome");
return View();
}
I've two action method in the following controller-
public class VisitMasterController
{
public ActionResult StartBrVisit()
{
string id=(Request.QueryString["id"].ToString(); //value=null here
}
public ActionResult BrNotPresent()
{
return RedirectToAction("StartBrVisit","VisitMaster" , new { id = "id", name = "name" });
}
{
After Redirect, Request.QueryString["id"] returns null.
My default route config is-
context.MapRoute(
"BR_default",
"BR/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "OpinionLeader.Areas.BR.Controllers" } //add this line
);
Any help?
You have defined a route with a parameter named id so when you use new { id = "id" }, the RedirectToAction() method finds a match and adds the value as a route value, not a query string value (in the case of name, there is no match, so its value is added as a query string value). You could access it using
string id = (string)Request.RequestContext.RouteData.Values["id"]
However, it would be far easier to add a parameter to your method
public ActionResult StartBrVisit(string id)
I am trying to construct an #Html.ActionLink to return to a previous page but to return to the previous page I need to pass in an id parameter.
ShowController/Details list a film defined by a int id, within this list is a link to go and look at the director details:
#Html.ActionLink( "(Director Bio)", "Index", "Director", new { searchString = item.Director.Name }, null)
When the user wants to go back to the film the StoreController/Details ActionResult is waiting for an int it.
I have tried to pass in the id to a viewBag:
public ActionResult Details(int id)
{
var item = from s in db.Shows.Where(si => si.ShowId == id) select s;
ViewBag.id = id;
return View(item);
}
And this shows fine in the Details view I cannot pick it up in the DirectorController/Index view to use in the actionLink, what do I do?
this is my action "Index"
when i first go to page i dont have the "pageParent"
so i dont get the page.
just if i enter to it like this "http://localhost:50918/Page?pageParent=1" it's enter.
how to make "http://localhost:50918/Page" to work?
public ActionResult Index(int pageParent)
{
var id = pageParent;
var pages = (from p in db.pages
where p.pageParent == id
select p);
PageParentModel model = new PageParentModel();
model.page = pages;
model.pageParent = id;
return View(model);
}
Modify your Action like this
public ActionResult Index(int? pageParent) {
// this way your pageParent parameter is marked to be nullable
// dont forget to check for the null value in code
}
You can set a default value for use too in the case that the parameter isn't supplied in the querystring - e.g.:
public ActionResult Index([DefaultValue(1)] int pageParent) {
}
I have repository class in asp.net mvc which has this,
public Material GetMaterial(int id)
{
return db.Materials.SingleOrDefault(m => m.Mat_id == id);
}
And my controller has this for details action result,
ConstructionRepository consRepository = new ConstructionRepository();
public ActionResult Details(int id)
{
Material material = consRepository.GetMaterial(id);
return View();
}
But why i get this error,
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'CrMVC.Controllers.MaterialsController'. To make a parameter optional its type should be either a reference type or a Nullable type.
Parameter name: parameters
Any suggestion...
You're getting the error because you're not passing an id to the controller method.
You basically have two options:
Always pass a valid id to the controller method, or
Use an int? parameter, and coalesce the null before calling GetMaterial(id).
Regardless, you should check for a null value for material. So:
public ActionResult Details(int? id)
{
Material material = consRepository.GetMaterial((int)(id ?? 0));
if (id == null)
return View("NotFound");
return View();
}
Or (assuming you always pass a proper id):
public ActionResult Details(int id)
{
Material material = consRepository.GetMaterial(id);
if (id == null)
return View("NotFound");
return View();
}
To pass a valid id to the controller method, you need a route that looks something like this:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id="" }
);
And an URL that looks like this:
http://MySite.com/MyController/GetMaterial/6 <-- id
It means the param (int id) was passed a null, use (int? id)
(in the controller)