Is viewbag the best option? - asp.net-mvc

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?

Related

How to return using ContentResult within a Navbar (partial View)

I am learning ASP.NET MVC C# at home with a basic C# programming. I am trying to create a list of customers, where I can click on each customer and it will show this customer name in a new view page. However, when a new viewpage appear, the customer name is there but no NavBar and other layout frames.
Here is my Code:
public ActionResult Details(int id)
{
var customers = GetCustomers().SingleOrDefault(c => c.Id == id);
if (customers == null)
return HttpNotFound();
return Content(customers.Name);
}
Because you are returning Content as result not view. You should have a view named Customer details named like Details.cshtml:
#model YourNameSpace.Models.Customer
<h1>#Model.Name</h1>
and in your controller action you should be returning a View :
public ActionResult Details(int id)
{
var customer = GetCustomers().SingleOrDefault(c => c.Id == id);
if (customer == null)
return HttpNotFound();
return View(customer);
}
Hope it gives you some clue how to approach it.

How to pass parameter to controller using actionlink

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);
}

How to pass the random item to the dropdown list of a view.

I am developing MCV app with Razor syntax.
I have pass the elements to the dropdown list and I want to pass the any random item to the view, as oer than item , dropdown list item will be selected.
below code displays the dropdow code.
Controller Code
[SessionFilterAction]
public ViewResult Details(int id)
{
ViewBag.HODList = new SelectList(db.Employees.Where(e => e.DesignationType == "HOD"), "Id", "FullName");
ViewBag.ItemToBeSelectedInList = 5;
return View(paymentadvice);
}
View Code
if(ViewBag.DesignationTypeOfLoggedUser == "Staff")
{
#Html.DropDownList("HODList", String.Empty ,new { ???? })
}
Now I want to use viewbag element which will be select the one of the item of dropdown.
How to do this ?
There's a constructor of the SelectList class which allows you to specify the id of the item to be selected:
[SessionFilterAction]
public ViewResult Details(int id)
{
int itemToBeSelectedInList = 5;
ViewBag.HODList = new SelectList(
db.Employees.Where(e => e.DesignationType == "HOD"),
"Id",
"FullName",
itemToBeSelectedInList
);
return View(paymentadvice);
}
This being said, using ViewBag is bad practice and I would recommend you switching to using view models and strongly typed helpers in the view.
#Html.DropDownList selects item with flag Selected (SelectListItem.Selected = true).
SelectList has constructor which automatically set this flag for specified item:
public SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue)
selectedValue should be the id of the employee that will be selected :
var employees = db.Employees.Where(e => e.DesignationType == "HOD").ToList();
var selectedEmployeeId = employess[5].Id;
ViewBag.HODList = new SelectList(employees, "Id", "FullName", selectedEmployeeId );

The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1

im trying to get the controller in my mvc application to edit a specific entity from a data model once the user clicks on the edit button, however I can't seem to make it work. I keep getting this error
The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1[MvcApplication1.Models.New]' but this dictionary requires a model item of type 'MvcApplication1.Models.New'.
what am I doin wrong. is it due to the strongly typed view??
here is my controller:
public ActionResult Edit(int id)
{
var productToEdit = from s in _entities.NewSet // return the story matching the clicked id
where s.storyId == id
select s;
return View(productToEdit);
}
// POST : Edit
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(New productToEdit)
{
try
{
var originalNews = (from s in _entities.NewSet
where s.storyId == productToEdit.storyId
select s).FirstOrDefault();
_entities.ApplyPropertyChanges(originalNews.EntityKey.EntitySetName, productToEdit);
_entities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
can someone give me a few pointers please. Im still new to all of this.
Change your Edit action with Int Parameter to as follows:
public ActionResult Edit(int id)
{
var productToEdit = from s in _entities.NewSet
where s.storyId == id
select s;
return View(productToEdit.FirstOrDefault());
}

overload ActionResult

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) {
}

Resources