Using Checkbox in mvc - c#

public class ProdList
{
public int ProdId { get; set; }
[DisplayName("Name")]
public string ProdName { get; set; }
[DisplayName("Price")]
public double? ProdPrice { get; set; }
[DisplayName("Description")]
public string ProdDesc { get; set; }
[DisplayName("Image")]
public string ImageVal { get; set; }
[DisplayName("Category Name")]
public string CateName { get; set; }
}
I have this class in my ProductListController. Using the following Action, I am populating my above class and passing it to the View.
public ActionResult ProdList()
{
Context con = new Context();
var prodlist = from p in con.Proddb join c in con.Catdb on p.CategoryID equals c.CategoryID select new ProdList() { ProdId=p.ProductID, ProdName = p.ProductName, ProdPrice = p.UnitPrice, ProdDesc=p.Description, CateName=c.CategoryName, ImageVal= "~/Image/"+p.ImagePath };
List<ProdList> prodlst = prodlist.ToList<ProdList>();
var categories = con.Catdb.ToList<Category>();
ViewBag.Categories = categories;
return View("ProdListView", prodlst);
}
My ProdListView includes the following list where I am printing the data:
#using (Html.BeginForm("checkboxhandle", "ProductList"))
{
#Html.AntiForgeryToken()
<table class="table table-condensed" id="prodtbl">
<tr>
<th>
#Html.DisplayNameFor(model => model.ImageVal)
</th>
<th>
#Html.DisplayNameFor(model => model.ProdName)
</th>
<th>
#Html.DisplayNameFor(model => model.ProdPrice)
</th>
<th>
#Html.DisplayNameFor(model => model.ProdDesc)
</th>
<th>
#Html.DisplayNameFor(model => model.CateName)
</th>
<th>
Check to Add
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
<img src="#Url.Content(#Html.DisplayFor(modelItem => item.ImageVal).ToString())" height="150" width="150">
</td>
<td>
#Html.DisplayFor(modelItem => item.ProdName)
</td>
<td>
#Html.DisplayFor(modelItem => item.ProdPrice)
</td>
<td>
#Html.DisplayFor(modelItem => item.ProdDesc)
</td>
<td>
#Html.DisplayFor(modelItem => item.CateName)
</td>
<td>
#*Checkbox*#
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.ProdId }) |
#Html.ActionLink("Details", "Details", new { id = item.ProdId }) |
</td>
</tr>
}
</table>
<div class="form-group" style="text-align:center">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Add to the Cart" class="btn btn-default" />
</div>
</div>
}
Following is the action method I am invoking once the submit button is clicked.
[HttpPost]
public ActionResult checkboxhandle(ProdList pdl)
{
/*Logic to get the checked items from the list in the view*/
}
I tried various methods to include checkbox and retrieve the checked items back in my controller but it didn't help. I'm really confused how to proceed. Please help me.

First of all add a bool property to your model/viewmodel:
public bool SelectedProduct {get;set;}
and then you need to do a for loop for the List<T>, so that model binder could populate items back when form posted like:
#for(int i=0; i< Model.Count; i++)
{
<tr>
<td>
<img src="#Url.Content(#Html.DisplayFor(modelItem => Model[i].ImageVal).ToString())" height="150" width="150">
</td>
<td>
#Html.DisplayFor(modelItem => Model[i].ProdName)
</td>
<td>
#Html.DisplayFor(modelItem => Model[i].ProdPrice)
</td>
<td>
#Html.DisplayFor(modelItem => Model[i].ProdDesc)
</td>
<td>
#Html.DisplayFor(modelItem => Model[i].CateName)
</td>
<td>
#Html.CheckBoxFor(modelItem => Model[i].SelectedProduct)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = Model[i].ProdId }) |
#Html.ActionLink("Details", "Details", new { id = Model[i].ProdId }) |
</td>
</tr>
}

Related

how do I get user input from the view and use it in my controller

I am trying to get the user's input PurchaseAmount from view and use that input for my logic, if the PurchaseAmount > CardFunds, then it'll print a message "Insufficient Funds", if the Purchase < CardFunds, it'll process to the next statement.
I declared the PurchaseAmount and CardFunds in my .cs file as such:
namespace Form6.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public partial class User
{
public int UserID { get; set; }
public int PurchaseAmount { get; set; }
public int CardFunds { get; set; }
}
}
PurchaseAmount will be given by the user, CardFunds is an Int datatype value previously storeed in the database.
and below is what I have for the controller :
namespace Form6.Controllers
{
public class LoginController : Controller
{
// GET: Login
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Authorize(Form6.Models.User userModel)
{
using (Form6Entities db = new Form6Entities())
{
var userDetails = db.Users.Where(x => x.Username == userModel.Username && x.Password == userModel.Password).FirstOrDefault();
var cardDetails = db.Users.Where(x=> x.FirstName == userModel.FirstName && x.LastName == userModel.LastName && x.CardNum == userModel.CardNum && x.ExpDate == userModel.ExpDate && x.CVV == userModel.CVV).FirstOrDefault();
var fundStatus = db.Users.Where(x => x.CardFunds == userModel.CardFunds).FirstOrDefault();
var purchaseAmount =
if (userDetails == null)
{
userModel.LoginErrorMessage = "Unauthorized for this transaction!";
return View("Index", userModel);
}
else if (PurchaseAmount > userModel.CardFunds)
{
userModel.LoginErrorMessage = "Insufficient Funds";
return View("Index", userModel);
}
else
{
Session["userID"] = userDetails.UserID;
return RedirectToAction("Index", "Home");
}
}
}
all the other logics are working properly such as verifying username and password, and all the cardDetaills inputs
how should I declare the variable PurchaseAmount in the controller with the user's input from the view file
and this will be the view's code:
<div id="login-div">
#using(Html.BeginForm("Authorize", "Login",FormMethod.Post))
{
<table>
<tr>
<td></td>
<td style="text-decoration:underline">Payment Gateway</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.Username)</td>
<td>#Html.EditorFor(model => model.Username)
<td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.Username)</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.Password) </td>
<td>#Html.EditorFor(model => model.Password)</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.Password)</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.FirstName) </td>
<td>#Html.EditorFor(model => model.FirstName)</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.FirstName)</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.LastName) </td>
<td>#Html.EditorFor(model => model.LastName)</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.LastName)</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.CardNum) </td>
<td>#Html.EditorFor(model => model.CardNum)</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.CardNum)</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.ExpDate) </td>
<td>#Html.EditorFor(model => model.ExpDate)</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.ExpDate)</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.CVV) </td>
<td>#Html.EditorFor(model => model.CVV)</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.CVV)</td>
</tr>
<tr>
<td colspan="2">
<label class="field-validation-error">#Html.DisplayFor(model =>model.LoginErrorMessage) </label>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="name" value="Submit" />
</td>
</tr>
</table>
}
How should I declare the variable PurchaseAmount in the controller with the user's input from the view file?
In this scenario you should have two models. One is your User Base Model and another for Purchase info Model. You can think of below model.
UserPurchaseinfo Model:
public class UserPurchaseinfo
{
public int UserID { get; set; }
public int PurchaseAmount { get; set; }
public int CardFunds { get; set; }
}
Note: As you already have User Model should I have renamed as UserPurchaseinfo to reduce ambiguity.
Modify Existing User Model:
public class UserModel
{
public string FullName { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string CardNum { get; set; }
public DateTime ExpDate { get; set; }
public string CVV { get; set; }
public UserPurchaseinfo UserPurchaseinfo { get; set; }
}
Note: As you noticed that we have added UserPurchaseinfo in UserModel class so that we can take input from same view.
View:
#model DotNet6MVCWebApp.Models.UserModel
<div id="login-div">
#using (Html.BeginForm("PostPaymetInfo", "YourControllerName", FormMethod.Post))
{
<table>
<tr>
<td></td>
<td><strong style="text-decoration:underline;margin-left:25px;">Payment Gateway</strong></td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.Username)</td>
<td>#Html.EditorFor(model => model.Username,new { htmlAttributes = new { #class = "form-control"}})
<td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.Username)</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.Password) </td>
<td>#Html.EditorFor(model => model.Password,new { htmlAttributes = new { #class = "form-control",type = "password"}})</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.Password)</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.FirstName) </td>
<td>#Html.EditorFor(model => model.FirstName,new { htmlAttributes = new { #class = "form-control"}})</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.FirstName)</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.LastName) </td>
<td>#Html.EditorFor(model => model.LastName,new { htmlAttributes = new { #class = "form-control"}})</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.LastName)</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.CardNum) </td>
<td>#Html.EditorFor(model => model.CardNum,new { htmlAttributes = new { #class = "form-control"}})</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.CardNum)</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.ExpDate) </td>
<td>#Html.EditorFor(model => model.ExpDate,new { htmlAttributes = new { #class = "form-control"}})</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.ExpDate)</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.CVV) </td>
<td>#Html.EditorFor(model => model.CVV,new { htmlAttributes = new { #class = "form-control"}})</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.CVV)</td>
</tr>
<tr>
<td></td>
<td><strong style="text-decoration:underline;margin-left:25px;">User Purchase Info</strong></td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.UserPurchaseinfo.PurchaseAmount) </td>
<td>#Html.EditorFor(model => model.UserPurchaseinfo.PurchaseAmount,new { htmlAttributes = new { #class = "form-control",type = "number"}})</td>
</tr>
<tr>
<td>#Html.LabelFor(model => model.UserPurchaseinfo.CardFunds) </td>
<td>#Html.EditorFor(model => model.UserPurchaseinfo.CardFunds,new { htmlAttributes = new { #class = "form-control",type = "number"}})</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="name" class="btn btn-success" value="Submit" />
</td>
</tr>
</table>
}
</div>
View Output:
Controller:
[HttpPost]
public ActionResult PostPaymetInfo(UserModel paymentModel)
{
// You can check and validate all the property which you are sending from your view
// Now Check your PurchaseAmount here whatever you are getting on paymentModel
return RedirectToAction("Index");
}
Final Output:
Basically you need to add your UserPurchaseinfo into your User Model so that you can access both property on your view finally you can take input from there and can submit above way.

Passing a parameter to Html.ActionLink

I am trying to pass a parameter to the Create #Html.ActionLink in the Index view however I am having some difficulties.
I have an Address controller in which the user sees the addresses of a specific person when they access the Index view. I would like to pass this PersonID to the Create view so that they do not have to select or enter the person when they create a new address. My actionlink looks like this -
#Html.ActionLink("Create New", "Create", new { id = Model.[PersonID is not a choice]})
My problem is that after Model PersonID is not an option. I am not sure how to get PersonID to the Create function in the AddressController.
I tried following along with the post - Passing a parameter to Html.ActionLink when the model is IEnumerable<T> - where they are having the same issue. The first answers seems like a likely solution however I could not duplicate the model they created and when I put the pieces in my Address model I could not duplicate the code they had performed in the controller. Is there another solution?
My Address Model -
public partial class Address
{
public int AddressID { get; set; }
public int PersonID { get; set; }
[Display(Name="Address")]
public string Address1 { get; set; }
[Display(Name = "Address 2")]
public string Address2 { get; set; }
public string City { get; set; }
[Display(Name="State")]
public string StateAbbr { get; set; }
[Display(Name = "Zip Code")]
public string Zip { get; set; }
public virtual Person Person { get; set; }
public List<Address> Address { get; set; }
}
My AddressController Index
public ActionResult Index(int id)
{
var addresses = db.Addresses.Include(a => a.Person)
.Where(a => a.PersonID == id);
Person person = db.People.Find(id);
ViewBag.FullName = person.FirstName + " " + person.LastName;
person.PersonID = id;
return View(addresses.ToList());
}
Address Index view -
#model IEnumerable<OpenBurn.Models.Address>
#{
ViewBag.Title = "Address";
}
<h2>Address</h2>
<p>
#Html.ActionLink("Create New", "Create", new { id = .PerosnID })
</p>
<h3 style="color: #008cba;"> #ViewBag.FullName </h3>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Address1)
</th>
<th>
#Html.DisplayNameFor(model => model.Address2)
</th>
<th>
#Html.DisplayNameFor(model => model.City)
</th>
<th>
#Html.DisplayNameFor(model => model.StateAbbr)
</th>
<th>
#Html.DisplayNameFor(model => model.Zip)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Address1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address2)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
<td>
#Html.DisplayFor(modelItem => item.StateAbbr)
</td>
<td>
#Html.DisplayFor(modelItem => item.Zip)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.AddressID }) |
#Html.ActionLink("Details", "Details", new { id=item.AddressID }) |
#Html.ActionLink("Delete", "Delete", new { id=item.AddressID })
</td>
</tr>
}
</table>
Since the #Html.ActionLink syntax isn't inside a foreach loop, you obviously can't use IEnumerable<OpenBurn.Models.Address> as the model. You need a new model class that contains a property that holds those Address records and a property that holds the PersonID value. You should also use the same model class to pass the FullName value instead of using ViewBag. I would suggest the below model class
public class AddressIndexModel
{
public AddressIndexModel()
{
this.Addresses = new List<Address>();
}
public int PersonID { get; set; }
public string FullName { get; set; }
public List<Address> Addresses { get; set; }
}
then change your controller to this
public ActionResult Index(int id)
{
var addresses = db.Addresses.Include(a => a.Person)
.Where(a => a.PersonID == id);
Person person = db.People.Find(id);
AddressIndexModel model = new AddressIndexModel();
model.PersonID = id;
model.FullName = person.FirstName + " " + person.LastName;
model.Addresses = addresses.ToList();
return View(model);
}
and change your view to below
#model AddressIndexModel
#{
ViewBag.Title = "Address";
}
<h2>Address</h2>
<p>
#Html.ActionLink("Create New", "Create", new { id = Model.PersonID })
</p>
<h3 style="color: #008cba;"> #Model.FullName </h3>
<table class="table">
<tr>
<th>
Address
</th>
<th>
Address 2
</th>
<th>
City
</th>
<th>
State
</th>
<th>
Zip Code
</th>
<th></th>
</tr>
#foreach (var item in Model.Addresses) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Address1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address2)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
<td>
#Html.DisplayFor(modelItem => item.StateAbbr)
</td>
<td>
#Html.DisplayFor(modelItem => item.Zip)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.AddressID }) |
#Html.ActionLink("Details", "Details", new { id=item.AddressID }) |
#Html.ActionLink("Delete", "Delete", new { id=item.AddressID })
</td>
</tr>
}
</table>

Enable boolean and enter text in view then pass back to controller - MVC

I have a view that displays a boolean (currently defaulted to 0) in a box format in the view that I cannot check to activate as true and also want to enter text in the result field to pass back to the controller and save both changes to a table. Can someone please explain what I have to do to allow this functionality to work please.
Controller code
public ActionResult P1A1Mark()
{
List<MarkModel> query = (from row in db.submits
where row.assignment_no.Equals("1") && row.group_no == 1
group row by new { row.assignment_no, row.student_no, row.student.firstname, row.student.surname } into g
select new MarkModel
{
student_no = g.Key.student_no,
student_surname = g.Key.surname,
student_firstname = g.Key.firstname
}
).ToList();
return View(query);
}
View
#model IEnumerable<MvcApplication2.Models.MarkModel>
#{
ViewBag.Title = "P1A1Mark";
}
<h2>Mark Student Assignments</h2>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.student_no)
</th>
<th>
#Html.DisplayNameFor(model => model.student_surname)
</th>
<th>
#Html.DisplayNameFor(model => model.student_firstname)
</th>
<th>
#Html.DisplayNameFor(model => model.submitted)
</th>
<th>
#Html.DisplayNameFor(model => model.result)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.student_no)
</td>
<td>
#Html.DisplayFor(modelItem => item.student_surname)
</td>
<td>
#Html.DisplayFor(modelItem => item.student_firstname)
</td>
<td>
#Html.DisplayFor(modelItem => item.submitted)
</td>
<td>
#Html.DisplayFor(modelItem => item.result)
</td>
</tr>
}
</table>
Model
public class MarkModel
{
public string student_no { get; set; }
public string student_surname { get; set; }
public string student_firstname { get; set; }
public string assignment_no { get; set; }
public bool submitted { get; set; }
public string result { get; set; }
public Nullable<int> group_no { get; set; }
}
Create an EditorTemplate for type of MarkModel.
In /Views/Shared/EditorTemplates/MarkModel.cshtml
#model MvcApplication2.Models.MarkModel
<tr>
<td>#Html.DisplayFor(m => m.student_no)</td>
<td>#Html.DisplayFor(m => m.student_surname)</td>
<td>#Html.DisplayFor(m => m.student_firstname)</td>
<td>#Html.CheckBoxFor(m => m.submitted)</td>
<td>#Html.TextBoxFor(m => m.result)</td>
</tr>
and in the main view
#model IEnumerable<MvcApplication2.Models.MarkModel>
#using (Html.BeginForm())
{
<table>
<thead>
// add your th elements
</thead>
<tbody>
#Html.EditorFor(m => m)
<tbody>
</table>
<input type="submit" ../>
}
and create a method to post back to
[HttpPost]
public ActionResult P1A1Mark(IEnumerable<MarkModel>model)
Alternatively you can use a for loop in the view (the model must be IList<T>)
for(int i = 0; i < Model.Count; i++)
{
....
#Html.CheckBoxFor(m => m[i].submitted)
}

Getting the UserName of an ApplicationUser into a view

I am trying to display the name of an author of a question but unlike the other properties of the model, the ApplicationUser.UserName property does not show in the view (it's just blank). I am using Entity Framework, MVC 5 and Razor.
My Question model:
public class Question
{
public int QuestionID { get; set; }
public string Title { get; set; }
public ApplicationUser Author { get; set; }
public string Description { get; set; }
[DisplayName("Created")]
public DateTime CreationDateTime { get; set; }
public int Rating { get; set; }
}
My QuestionController Index action:
// GET: Questions
public ActionResult Index()
{
return View(db.Questions.ToList());
}
And the view:
#model IEnumerable<Waegogi.Models.Question>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Title)
</th>
<th>
#Html.DisplayNameFor(model => model.Author.UserName)
</th>
<th>
#Html.DisplayNameFor(model => model.Description)
</th>
<th>
#Html.DisplayNameFor(model => model.CreationDateTime)
</th>
<th>
#Html.DisplayNameFor(model => model.Rating)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Author.UserName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.DisplayFor(modelItem => item.CreationDateTime)
</td>
<td>
#Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.QuestionID }) |
#Html.ActionLink("Details", "Details", new { id=item.QuestionID }) |
#Html.ActionLink("Delete", "Delete", new { id=item.QuestionID })
</td>
</tr>
}
</table>
Where am I going wrong?
You should be able to solve this by explicitly loading your relationships in your controller call:
public ActionResult Index()
{
return View(db.Questions.Include(q => q.ApplicationUser).ToList());
}

MVC Model binding on multiple edit

I have the following view model
public class ResourceProjectedCapacitiesModel
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Location { get; set; }
public string Manager { get; set; }
public string Role { get; set; }
public string Project { get; set; }
public decimal Capacity { get; set; }
public System.DateTime Date { get; set; }
}
In my GET action i display all these in the view.
[HttpPost]
public ActionResult Index(List ResourceProjectedCapacitiesModel> list)
{
foreach (ResourceProjectedCapacitiesModel item in list)
{
....
}
....
}
In the view there is the following editor but when the form is submitted the binding doesnt work, list is null
#Html.EditorFor(modelItem => item.Capacity)
#Html.HiddenFor(modelItem => item.ID)
#model List<CapacityPlanner.ViewModels.ResourceProjectedCapacitiesModel>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
#using(Html.BeginForm())
{
<table class="table">
<tr>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
Location
</th>
<th>
Manager
</th>
<th>
Role
</th>
<th>
Project
</th>
<th>Apr</th>
<th>May</th>
<th>Jun</th>
<th>Jul</th>
<th>Aug</th>
<th>Sep</th>
<th>Oct</th>
<th>Nov</th>
<th>Dec</th>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Location)
</td>
<td>
#Html.DisplayFor(modelItem => item.Manager)
</td>
<td>
#Html.DisplayFor(modelItem => item.Role)
</td>
<td>
#Html.DisplayFor(modelItem => item.Project)
</td>
#for(var i = 1; i <= 12; i++)
{
<td>
#if(item.Date.Month == i)
{
#Html.EditorFor(modelItem => item.Capacity)
}
</td>
}
<td>
#Html.HiddenFor(modelItem => item.ID)
</td>
</tr>
}
</table>
<input type="submit" value="Save" class="btn btn-default" />
}
I have inserted the code of my view
You should use for instead of foreach.
#for (int i = 0; i < Model.Items.Count; i++)
{
#Html.EditorFor(model => Model.Items[i].Amount)
#Html.HiddenFor(model => Model.Items[i].Amount)
}
Then all filled will get different id.

Categories