How do I get my view to recognize my object - c#

I already have a view created that I want to use to display results being pulled from the db and passed to the view in my action. When I created the view I didn't tell it what objects I wanted to pass in so I'm trying to add them manually but the view doesn't seem to recognize them.
Here is my Results view and the top line is what I'm trying to add, but VS doesn't recognize it. So spaces is red (unrecognized) and item.Name and item.description are red
#spaces IQueryable<GiftExchange.Core.Models.Space.YogaSpace>
#{
ViewBag.Title = "Results";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h3>Search Results</h3>
#using (Html.BeginForm("Results", "Home", FormMethod.Get))
{
<p>
#Html.TextBox("Location") <input value='Search' type="submit" />
</p>
}
<table border="1">
<tr>
<th>
#Html.DisplayNameFor(spaces => spaces.Name)
</th>
<th>
#Html.DisplayNameFor(spaces => spaces.Description)
</th>
</tr>
#foreach (var item in spaces)
{
<tr>
<th>
#Html.DisplayFor(modelItem => item.Name)
</th>
<th>
#Html.DisplayFor(modelItem => item.Description)
</th>
</tr>
}
</table>
Here is my action
[AllowAnonymous]
public ActionResult Results(string searchTerm)
{
IQueryable<YogaSpace> spaces;
using (var repo = new YogaSpaceRepository())
{
spaces = repo.All;
}
return View(spaces);
}

In the first line of your view, switch #spaces for #model.
And then where you want to use it, instead of spaces, use Model.
For more details, google for "strongly typed views".

Related

How to pass model as a parameter to [HttpPost] ActionMethod in ASP.NET MVC?

I have a usual grid which has edit and save button. When I click the Edit link from the grid it goes to Edit action method, where I can read the Id as a parameter. I am thinking since I can pass the Id then why not the whole model as a parameter to the Edit action? However, I am always getting null. I looked at some examples they all shows passing the id rather than the model object. I wonder why model cannot be passed? Even though when I debug the code I always see item row is binding with the parameter Student from item.
View for Grid with Edit link:
#model IEnumerable<MVC_BasicTutorials.Models.Student>
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.StudentName)
</th>
<th>
#Html.DisplayNameFor(model => model.Age)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.StudentName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Age)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.StudentId, std=item }) |
</td>
</tr>
}
</table>
Controller action method:
// Get
public ActionResult Edit(int Id, Student std)
{
var checkSet=std; // coming null
var checkId=Id; //i am seeing the id value
return RedirectToAction("Index");
}
Try the following:
public ActionResult Index()
{
var model = /* prepare view data model */;
return View(model);
}
[HttpPost]
// The second parameter `Id` is removed because of the `Student` already contains it.
public ActionResult Edit(Student std)
{
System.Diagnostics.Debug.WriteLine($"Id= {std.StudentId}, Student Name= {std.StudentName} ");
return RedirectToAction("Index");
}
In the Index.cshtml you can use the Html.BeginForm helper method to specify that the target of the form is the 'Edit' action method.
#model IEnumerable<MVC_BasicTutorials.Models.Student>
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.StudentName)
</th>
<th>
#Html.DisplayNameFor(model => model.Age)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
using (Html.BeginForm("Edit", "Home"))
{
#* Prepare a hidden data context to the able the MVC data binding work correctly *#
#Html.Hidden("StudentId", item.StudentId)
#Html.Hidden("StudentName", item.StudentName)
#Html.Hidden("Age", item.Age)
<tr>
<td>
#Html.DisplayFor(modelItem => item.StudentName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Age)
</td>
<td>
#*#Html.ActionLink("Edit", "Edit", new { id = item.StudentId, std = item }) *#
<input type="submit" value="Edit" />
</td>
</tr>
}
}
</table>

How to use EditorFor to save changes to the Model?

I have a view like this:
#model IEnumerable<Namespace.MyClass>
#using Newtonsoft.Json;
<form asp-action="Update">
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Product)
</th>
<th>
#Html.DisplayNameFor(model => model.IsAvailable)
</th>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Product)
</td>
<td>
#Html.EditorFor(modelItem => item.IsAvailable)
</td>
</tr>
}
</tbody>
</table>
<div>
<a href=#Url.Action("SaveChanges", "Product", new {productList = JsonConvert.SerializeObject(Model) })> Save changes</a>
</div>
</form>
where SaveChanges looks like this:
[HttpGet] // Perhaps it should be HttpPost here, but if I change it to that nothing happens when I run the program after clicking the "Save changes" link
public ActionResult SaveChanges(string productList)
{
List<MyClass> products = JsonConvert.DeserializeObject<List<MyClass>>(productList);
// Here I continue with SqlCommand to later make an UPDATE to a database changing the value of IsAvailable which is the only adjustable value in my view
}
My issue is that when I run the program and I change IsAvailable (which is a bool) from false to true in my view and press "Save Changes", the model does not change, i.e. productList still has the value False for IsAvailable for that particular product.
Does anybody have an idea as to why? From my understanding if I have a and
Model information is not updated when you send the model to the action with the link. And the same initial amount is sent.
The changes you make to the model fields are only in the html controls and have nothing to do with the model. The model is updated when you post the form information.
I submitted the form to jQuery in the following code
in view
#model List<Namespace.MyClass>
#using (Html.BeginForm("SaveChanges", "Product", FormMethod.Post))
{
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.FirstOrDefault().Product)
</th>
<th>
#Html.DisplayNameFor(model => model.FirstOrDefault().IsAvailable)
</th>
</thead>
<tbody>
#for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
#Html.DisplayFor(modelItem => Model[i].Product)
</td>
<td>
#Html.EditorFor(modelItem => Model[i].IsAvailable)
</td>
</tr>
}
</tbody>
</table>
<div>
#Html.ActionLink("Save changes", "", null, new { #id = "submit"})
</div>
}
#section scripts{
<script type="text/javascript">
$("#submit").click(function () {
document.forms[0].submit();
return false;
});
</script>
}
action in controller
[HttpPost] // Perhaps it should be HttpPost here, but if I change it to that nothing happens when I run the program after clicking the "Save changes" link
public ActionResult SaveChanges(IEnumerable<MyClass> products)
{
// Here I continue with SqlCommand to later make an UPDATE to a database changing the value of IsAvailable which is the only adjustable value in my view
}

I can't see the input value in the input in html controller

The model of my html page is in the form of a list. I want to enter the exam grade here, but when I entered the exam grade, my data does not go to my actionResult type function. In fact, when I write an integer value to the input, it does not save its value. The database also lists the values I have written manually, but still does not bring the data to the function.
my html code:
#model IEnumerable<OnlineBasvuru.Entity.ViewModel.ExamViewModal>
#{
Layout = null;
}
<div>
#using (Html.BeginForm("Save", "Exam", FormMethod.Post))
{
<table class="table">
<thead>
<tr>
<th>
StudentId
</th>
<th>
Note
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.ToList())
{
<tr>
<td>
#Html.Label(item.StudentId.Value.ToString())
</td>
<td>
#Html.EditorFor(modelItem => item.Note)
</td>
</tr>
}
</tbody>
</table>
<button type="submit">Save</button>
}
</div>
my controller code:
[HttpPost]
public ActionResult Save(ICollection<ExamViewModal> nwModal) // Normally nwModal should be listed as a list of the model and the data should be in
{
foreach (ExamViewModal modal in nwModal)
{
EXAM exam = examService.Get(modal.Id);
exam.NOTE = modal.Note;
examService.Save(exam );
}
return RedirectToAction("ExamList", "Exam");
}
Please show me a way I do not know how to do. Thank you.
Try using a for loop instead of a foreach. This will then set the name property using indexes rather than spitting out the same name for each item in the loop.
For example:
for (int i = 0; i < Model.Count(); i++) {
Html.EditorFor(m => Model[i].Note)
}

Accessing a field from a differen't model's table in ASP.NET MVC

I have a page that displays a filtered table based on the user that is passed to the page. I also want to display the user's info a the top of the page, but this comes from a different table (from the Account model). Is there a way I can access fields from this table while working in my other controller/views?
Here is my code:
#{
ViewBag.Title = "User Profile";
}
#model IEnumerable<FTv2.Models.Trade>
#{
ViewBag.Title = "Active Trades";
ViewBag.ImgUrl = ViewBag.Name + ".png";
}
<h2>#ViewBag.User's Profile:</h2>
<p>
// This is where I would like to put the User info.
</p>
<h2>Active Trades:</h2>
<p>
</p>
<table>
<tr>
<th>
</th>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Price)
</th>
<th>
#Html.DisplayNameFor(model => model.User)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
if (item.User == ViewBag.User)
{
<tr>
<td>
#{ViewBag.ImgUrl = #item.Name + ".png";}
<img src="/Images/#ViewBag.ImgUrl" HEIGHT="66" WIDTH="50" >
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Price) TE
</td>
<td>
#Html.DisplayFor(modelItem => item.User)
</td>
#{if (ViewBag.User == User.Identity.Name){
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
#Html.ActionLink("Details", "Details", new { id = item.ID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
}}
</tr>
}
}
</table>
There are two options that I can think of:
Render an action method that returns the user details
#{
Html.RenderAction("UserDetails", new { UserID = ViewBag.UserID});
}
Create a new model that has the user details plus an IEnumerable of the trades and use this for your view.
I suppose that you wanna
make page model that will contain the User Info and IEnumerable<...>.
or pass User info into ViewBag
or call #Html.RenderAction()
I think that you need to keep in mind the chance of reuse of the view model class. If you see no way to reuse the model then use ViewBag to pass the info into a View. In the Controller's Action set the property ViewBag.UserInfo = userInfo; and in the view call: code#Html.Partial("_UserInfo", (UserInfo)ViewBag.UserInfo)code.
But more practical way is to call #Html.Action():
1. Make action UserController.UserInfo(int id).
2. Call from your view #Html.Action("UserInfo", "User", new {Id = ViewBag.UserId});
3. create partial view for the UserController.UserInfo action.

MVC 4 How to pass an object to partial view when list item is clicked?

This is a controller
public class ProductController : Controller
{
//
// GET: /Product/
public ActionResult Index(int id = 0)
{
return View(DALCategory.GetCategoryList());
}
public PartialViewResult productPartial(int id)
{
if (id > 0)
{
return PartialView("_productPartial", DALProduct.GetProductListByCateDetail(id));
}
else
{
return PartialView("_productPartial", DALProduct.GetProductList());
}
}
This is code in index view
#model IEnumerable<Model.Category>
<h2>Products</h2>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.name)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.ActionLink(#item.name, "productPartial", new { id = item.id })
</td>
</tr>
}
</table>
#Html.Partial(productPartial)
And this is code for partial view.
#model IEnumerable<Model.Product>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.SKU)
</th>
<th>
#Html.DisplayNameFor(model => model.product_name)
</th>
<th>
#Html.DisplayNameFor(model => model.price)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.SKU)
</td>
<td>
#Html.DisplayFor(modelItem => item.product_name)
</td>
<td>
#Html.DisplayFor(modelItem => item.price)
</td>
<td>
#* #Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
#Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
#Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })*#
</td>
</tr>
}
</table>
So what I want to do is... when an item is clicked, it renders the partial with an new object. Yes, the partial view is only rendered on the same page.
I have been doing so many things. Although I do not think it is such a hard problem, I am not really sure what I need to do for this...
Since you want to update a part of the page you will need to use ajax to accomplish that. You have two options there:
User #Ajax.ActionLink (How can i load Partial view inside the view)
You can write your own jquery ajax request that invokes your controller method and returns the partial view.
It happened to me as well. I wanted the page to be partial and loaded on the same page but it kept forwarding me to other link. Make sure following points are taken care of:
You have included your jquery-1.6.2.js or whatever version of this file you have.
Also make you have included your jquery file you have created. Make sure the above one is included first.
If you have included these files in some section of your .cshtml section then make sure you render it in your _Layout.cshtml or other layout file you are using. For example you have included your file in head section of your index.cshtml like this
#section head{
}
then you have to render this section in _layout.cshtml with #RenderSection("head", required: false)
what you can also do is Make sure that jQuery is not included twice and that you don't have some javascript errors. Look at your javascript console in the browser as well as the Network tab to ensure that all your scripts are included properly.
Good Luck!!!

Categories