show details view in mvc 3 - asp.net

HI i am new to mvc so if anyone can help me will be great
basically i just want to display a partiular item details, my code
private sneakerEntities
_sneaker_categoryDataModel = new sneaker_info.Models.sneakerEntities();
public ActionResult Index()
{
IList<sneaker> Releases = _sneaker_categoryDataModel.sneakers.ToList<sneaker_info.Models.sneaker>();
return View("Releases", Releases);
}
//
// GET: /Home/Details/5
public ActionResult Details (int id)
{
return View();
}

You need to find it from Repository and display in your strongly typed view
public ActionResult Details (int id)
{
Sneaker snkr=_sneaker_categoryDataModel.sneakers.Find(id);
return View(snkr);
}
In Details.cshtml
#model Sneaker
//all view related code

private sneakerEntities
_sneaker_categoryDataModel = new sneaker_info.Models.sneakerEntities();
public ActionResult Index()
{
IList<sneaker> Releases = _sneaker_categoryDataModel.sneakers.ToList<sneaker_info.Models.sneaker>();
// populate your IList with values here
return View("Releases", Releases);
}
After this Add a View By right clicking on this Action.
You will have a view Created which will have the First line as
#model IList<sneaker>
if not change it to this. Now write this syntax to retrieve values from each item of the list
#foreach(var data in Model)
{
//#data.Whatever
#data.Name
}

Related

Multiple models in same view in asp.net mvc 4

I'm using asp.net mvc 4 & Entity Framework 6 to make a website where after user login, the table records will show & the ID of the user will show as Session. But when I run, after login, I get this error, Object reference not set to an instance of an object. I've made a custom model where two DbSet from seperate EF6 models are attached. My code is below,
Custom Model
public class MkistatVsUserLogin
{
public sample_1 sample_1 { get; set; } //Login Model
public IEnumerable<mkistat> mkistats { get; set; } //Table Model
}
Controller
[HttpPost]
public ActionResult Login(sample_1 id)
{
if (ModelState.IsValid)
{
var uservar = db.sample_1.Where(a => a.boid.Equals(id.boid)).FirstOrDefault();
if (uservar != null)
{
Session["UserBOID"] = uservar.boid.ToString();
return RedirectToAction("UserLogin");
}
}
var mkimodel = new MkistatVsUserLogin { mkistats = dsedb.mkistats.ToList() };
return View(id);
return View(mkimodel);
}
View
#model ABCoLtd.Models.MkistatVsUserLogin
#if (Session["UserBOID"] != null)
{
<li>Welcome, <b>#Session["UserBOID"].ToString()</b></li>
}
<a class="btn btn-default" href="#Url.Action("UserLogout", "Home")">Log Out</a>
#foreach (var item in Model.mkistats)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.MKISTAT_ID)
</td>
<td>
#Html.DisplayFor(modelItem => item.MKISTAT_PHONE_NO)
</td>
</tr>
}
Is there something wrong with my code? If it is then please give me a solution. All I want is to use both models at the same view where user login model will be used in Session & table model to list all the records.
Remove bellow line
return View(id);
and in your return View line also mention View or ActionMethod name
return View("ViewName",mkimodel)
In your action method, Use Session to pass ID
Session["UserID"] = id;
You can use the same in your View as :
<html>
-----Your CODE
<body>
------Your CODE
#Session["UserID"]
------Your CODE
</body>
</html>
You can create a ViewModel to handle :
namespace ABCoLtd.ViewModels
{
public class LoginVM
{
public MkistatVsUserLogin MkistatVsUserLogin {get;set;}
public int Id {get;set;}
}
}
and in action pass this way:
var mkimodel = new MkistatVsUserLogin { mkistats = dsedb.mkistats.ToList() };
LoginVM vm = new LoginVM();
vm.MkistatVsUserLogin = mkimodel ;
vm.Id = id;
return View(vm );
and in View set model to LoginVM:
#model ABCoLtd.ViewModels.LoginVM
UPDATE:
you already have a viewmodel didnt noticed that, you just need to do this:
var mkimodel = new MkistatVsUserLogin
{
mkistats = dsedb.mkistats.ToList(),
sample_1 = id
};
return View(nkimodel);
After login you are directly redirecting your user to an action and that action you have not mentioned here.And that action whose view you have shown is an strongly typed view which expects a list of MkistatVsUserLogin. But, I think you are not passing that list, so Model property of view will remain null and that will be causing an error of object reference. Please check this issue.
I got it. I just made another method in controller for viewing table with user session.
[HttpPost]
public ActionResult Login(sample_1 id)
{
if (ModelState.IsValid)
{
var uservar = db.sample_1.Where(a => a.boid.Equals(id.boid)).FirstOrDefault();
if (uservar != null)
{
Session["UserBOID"] = uservar.boid.ToString();
return RedirectToAction("UserLogin");
}
}
return View(id);
}
public ActionResult UserLogin()
{
if(Session["UserBOID"] != null)
{
var mkimodel = new MkistatVsUserLogin { mkistats = dsedb.mkistats.ToList() };
return View(mkimodel);
}
else
{
return RedirectToAction("Login");
}
}

ASP.NET MVC passing query result from controller to view

How to print the result of query in View page for ASP.NET MVC?
My code is:
public ActionResult Index()
{
var list = from m in db.MenuTables
select m.MenuName;
return View(list);
}
Now what should i write to print the result of this query in View Page?
Personally, I would get in the habit of having ViewModels and then strongly typing your View, to that model.
The model will expose ONLY THE DATA you want to display. Nothing more, nothing less. So let's assume you want to display the Name, Price and some other meta data.
Pseudo-code:
//View Model
public class MenuItem
{
public string Name { get; set; }
public decimal Price { get; set; }
public bool IsVegetarian { get; set; ]
}
public class IndexViewModel
{
public IList<MenuItem> MenuItems { get; set; }
public string MaybeSomeMessage { get; set; }
}
//in Controller
public ActionResult Index()
{
// This gets the menu items from your db, or cache or whatever.
var menuItemsFromDb = GetMenuItems();
// Let's start populating the view model.
IndexViewModel model = new IndexViewModel();
// Project the results to your model.
IList<MenuItems> menuItems = null;
if (menuItemsFromDb != null)
{
model.MenuItems = (from menuItem in menuItemsFromDb
select new MenuItem() {
Name = menuItem.Name,
Price = menuItem.Price,
IsVegetarian = menuItem.IsVegetarian
}).ToList();
}
// Anything else...
model.MaybeSomeMessage = "Hi There!";
return View(model);
}
//in View
#model IndexViewModel
<h3>#Model.MaybeSomeMessage</h3>
<ul>
#foreach(var item in Model.MenuItems)
{
<li>#item.Name - $ #item.Price</li>
}
</ul>
etc..
Note I've skipped some error checking, etc.
The point: only pass what you need.
At first, you may think this is much more code than is necessary. The best answer I can suggest to that thought, is that in the long run, you'll thank yourself for getting in the habit of this because the view should only ever know about the exact data it requires.
Nothing more, nothing less. Sending the least amount of data means you have a very light and simple view which will make your support/debugging much better. Next, you'll be able to unit test your controllers with a lot more intelligence and smarts, when you get to that.
Assuming that list is an IEnumerable of strings (i.e. that MenuName is a string).
In your view, accept the model IEnumerable<string>
#model IEnumerable<string>
and then enumerate it
#foreach( string s in Model )
{
<div>
#s
</div>
}
The first thing you want to do is call ToList() or else you could possibly be executing the same SQL query multiple times.
public ActionResult Index()
{
var list = (from m in db.MenuTables
select m.MenuName).ToList();
return View(list);
}
Secondly, I wouldn't just pass up a full list like that. You should create a ViewModel. That will allow you to pass up more data later on with a smaller effort.
public ActionResult Index()
{
var model = new IndexModel();
model.Tables = db.MenuTables.ToList();
model.AnotherValue = "MENUS";
return View(model);
}
Now we are on the view, you will need to set the model and iterate the table.
#model IndexModel
<h3>#Model.AnotherValue</h3>
<ul>
#foreach( var table in Model.Tables) {
<li>#table.Name<li>
}
</ul>
public ActionResult Index()
{
var list = from m in db.MenuTables
select m.MenuName;
return View(list);
}
//In View
#model IEnumerable<ProjectName.models.MenuTables>
#foreach(var item in Model)
{
#item.Field_Name
}

How to edit using ViewData model

I have created a View Model based on combination of three Tables.
I click on Edit action it displays the Data from Three tables correctly.
But when i click on Save button i am not able to get data either from FormCollection or from Request["Id"]
Please suggest it the possible way.
public class ConferenceResourceEditModel
{
public ConferenceRoom ConferenceRoom { get; set; }
public Resources Resources { get; set; }
public ResourceAllocation ResourceAllocation { get; set; }
}
public ActionResult Edit(int id)
{
//ConferenceRoom conferenceroom = db.ConferenceRooms.Find(id);
var query =
from c in db.ConferenceRooms
from r in db.Resourcess
from ra in db.ResourceAllocation
where c.ConferenceID == id
where c.ConferenceID == ra.ConferenceID
where r.ResourceID ==ra.ResourceID
select new ConferenceResourceEditModel { ConferenceRoom = c, Resources = r,ResourceAllocation=ra };
return View(query);
}
//
// POST: /ConferenceRoom/Edit/5
[HttpPost]
public ActionResult Edit(FormCollection form, int id, ConferenceResourceEditModel conferenceroom,ConferenceRoom crf)
{
if (ModelState.IsValid)
{
db.Entry(conferenceroom).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(conferenceroom);
}
Look up model binding on the http://www.asp.net/mvc site where there are magnificent tutorials about this kind of thing.
in a nutshell your controller action will take a parameter of type YOURVIEWMODEL and bind to it automatically
Use some ORM ( like EntityFramework,personally i recommend DatabaseFirst approach). Using something like that:
var query =
from c in db.ConferenceRooms
from r in db.Resourcess
from ra in db.ResourceAllocation
where c.ConferenceID == id
where c.ConferenceID == ra.ConferenceID
where r.ResourceID ==ra.ResourceID
is much more complicated and difficult. Hope it will help.

Is there a variable to inform Edit Action if I come from Create in mvc?

Using mvc3, is there some variable to inform if I came from a Create to Edit action?
If the routing is used as well in a GET I want a certain behavior, otherwise if the CreateAction has been fired I want to open a different context Edit.
Failed first solution: Creating a extra Action Edit, fires: The current request for action 'Edit' on controller type 'XController' is ambiguous between the two action methods.
More important, is there a misinterpretation of MVC? Does this solution sound weird to anyone? I'm facing nice doubts with mvc. :)
You can either add an optional parameter to your Edit action that flags whether or not you came from the Add action or you could create an entirely new action (with a unique name from Edit).
The former would look something like:
public ActionResult Edit(int id, bool? fromCreate = false)
{
if(fromCreate)
{
// do your special processing
}
}
And the latter, obviously, would be:
public ActionResult Edit(int id)
{
}
public ActionResult EditNewlyCreated(int id)
{
}
In your create view or create controller action (no razor markup if its in the action):
#{ TempData["FromCreate"] = true; }
And then in your edit get action:
public ActionResult Edit()
{
if( TempData.ContainsKey("FromCreate") )
{
var fromCreate = (bool)TempData["FromCreate"];
TempData.Remove("FromCreate");
if( fromCreate )
{
//insert logic
}
}
}
If you have something linke this in MyView view:
#model ModelClass
#Html.BeginForm()
{
if(Model.Id > 0)
{
#Html.HiddenFor(m => m.Id);
}
...
}
And in controller:
public ActionResult Edit(int id)
{
var model = modelRepository.GetById(id);
Return View("MyView", model);
}
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
var model = modelRepository.GetById(id);
TryUpdateModel(model)
{
modelRepository.Save(model);
}
}
public ActionResult Create()
{
var model = new ModelClass();
Return View("MyView", model);
}
[HttpPost]
public ActionResult Create(ModelClass model)
{
...
}
In view, if Model.Id > 0 it means that we entered view using Edit action and when form posts, if there will be Id field in post parameters (hidden for id) then Edit(with HttpPost) will be called, else if there will not be Id parameter then Create action will be called
If you have 2 Edit methods, they must have different method inputs to differentiate them.
public ActionResult Edit(int id)
{
return View(db.GetWidget(id));
}
public ActionResult Edit(int id, string username)
{
ViewBag.Username = username;
return View(db.GetWidget(id));
}
Or, just make the method into one with an optional parameter
public ActionResult Edit(int id, string username = "")
{
ViewBag.Username = username;
return View(db.GetWidget(id));
}
I would also recommend painting a method attribute such as [HttpGet], [HttpPost], etc.

MVC: Member and Member.Items.add(Item) doesn't get saved

The item doesn't get saved. Only member gets saved.
When I debug [AcceptVerbs(HttpVerbs.Post)]
the information is empty in the Item. Why? How should I solve this?
When it goes to the post method of create, then the ITEM dosen't follow the member. and the ITEMS doesn't get saved. I debug the information and there are 0 Number items. Why doesn't it save Items also when I press the button.
Only the member item gets saved.
public class ClassifiedsFormViewModel
{
IClassifiedsRepository classifiedsRepository = new ClassifiedsRepository();
public Member Member { get; private set; }
public SelectList Municipalities { get; private set; }
public ClassifiedsFormViewModel(Member member)
{
Member = member;
Municipalities = new SelectList(classifiedsRepository.GetMunicipalities()
,"MunicipalityId", "Municipality1");
}
}
public ActionResult Create()
{
Member member = new Member();
Item item = new Item();
member.Items.Add(item);
return View(new ClassifiedsFormViewModel(member));
}
//
// POST: /Items/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Member member)
{
if (ModelState.IsValid)
{
try
{
classifiedsRepository.Add(member);
classifiedsRepository.Save();
return RedirectToAction("Create", new { id = member.MemberId });
}
catch
{
ModelState.AddModelErrors(member.GetRuleViolations());
}
}
return View(new ClassifiedsFormViewModel(member));
}
}
The member which is passed into the create function is actually databound from your form. In order to ensure that it works you have to have the elements on the form named the same as the properties in your member. So if you have something called memberName in Member you would need to name the field in the view the same thing.
<form ...
<input type="text" name="memberName"/>
...
</form>
Edit:
After reading your comments I'm still not 100% what you're looking to do. IF you want the member you've created to have an item then move the item creation code down to the second Create. What you have passes a member with an item through to a strongly typed view. The member with the item is never persisted so it won't come back to the controller and won't make it into the model.
public ActionResult Create()
{
Member member = new Member();
Item item = new Item();
member.Items.Add(item);
return View(new ClassifiedsFormViewModel(member));
}

Resources