Getting null values from model - c#

Trying to get this to work but keep getting null values from the Model.
Controller:
[HttpPost]
public ActionResult Index(OPISPriceReportOLY_Result model)
{
if (ModelState.IsValid)
{
int id = model.orpid;
using (var context = new IntranetCoreEntities())
{
var selected = context.OPISRetailPricings.Find(id);
selected.DMarkup = model.DMarkup;
selected.DSell = model.DSell;
selected.RMarkup = model.RMarkup;
selected.RSell = model.RSell;
context.SaveChanges();
}
}
return View("Index", model);
}
View:
#model IEnumerable<OPIS7.Models.OPISPriceReportOLY_Result>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#using (Html.BeginForm("Index", "OPISPriceReportOLY_Result", FormMethod.Post))
{
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.cpid)
</th>
<th>
#Html.DisplayNameFor(model => model.Zone)
</th>
<th>
#Html.DisplayNameFor(model => model.ZoneDescription)
</th>
<th>
#Html.DisplayNameFor(model => model.Rack)
</th>
<th>
#Html.DisplayNameFor(model => model.ActualProduct)
</th>
<th>
#Html.DisplayNameFor(model => model.Cost)
</th>
<th>
#Html.DisplayNameFor(model => model.DMarkup)
</th>
<th>
#Html.DisplayNameFor(model => model.DSell)
</th>
<th>
#Html.DisplayNameFor(model => model.RMarkup)
</th>
<th>
#Html.DisplayNameFor(model => model.RSell)
</th>
<th>
#Html.DisplayNameFor(model => model.DateUpdated)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.cpid)
</td>
<td>
#Html.DisplayFor(modelItem => item.Zone)
</td>
<td>
#Html.DisplayFor(modelItem => item.ZoneDescription)
</td>
<td>
#Html.DisplayFor(modelItem => item.Rack)
</td>
<td>
#Html.DisplayFor(modelItem => item.ActualProduct)
</td>
<td>
#Html.DisplayFor(modelItem => item.Cost)
</td>
<td>
#Html.TextBoxFor(modelItem => item.DMarkup)
</td>
<td>
#Html.TextBoxFor(modelItem => item.DSell)
</td>
<td>
#Html.TextBoxFor(modelItem => item.RMarkup)
</td>
<td>
#Html.TextBoxFor(modelItem => item.RSell)
</td>
<td>
#Html.DisplayFor(modelItem => item.DateUpdated)
</td>
<td>
<button type="submit">Update</button>
</td>
</tr>
}
</table>
}
Model:
namespace OPIS7.Models
{
using System;
using System.ComponentModel.DataAnnotations;
public partial class OPISPriceReportOLY_Result
{
[Key]
public int orpid { get; set; }
public int cpid { get; set; }
public string Zone { get; set; }
public string ZoneDescription { get; set; }
public string Rack { get; set; }
public string ActualProduct { get; set; }
public Nullable<double> Cost { get; set; }
public Nullable<double> DMarkup { get; set; }
public string DSell { get; set; }
public Nullable<double> RMarkup { get; set; }
public Nullable<double> RSell { get; set; }
public Nullable<System.DateTime> DateUpdated { get; set; }
}
}
According to documentation this is supposed to work without having to resort to AJAX or JS of any kind but I'm hitting a wall. Any ideas?

If you just want to take single OPISPriceReportOLY_Result in action method, you will need to move form tag inside for loop.
The clean approach is to create a Partial View. You can read more at Adam Freeman's book.
Index.cshtml
#model IEnumerable<OPISPriceReportOLY_Result>
<table class="table">
#foreach (var item in Model)
{
#Html.Partial("_Result", item)
}
</table>
_Result.cshtml
#model OPISPriceReportOLY_Result
#using (Html.BeginForm("Update", "Home", FormMethod.Post))
{
<tr>
<td>
#Html.DisplayFor(x => x.cpid)
#Html.HiddenFor(x => x.cpid)
</td>
<td>
#Html.DisplayFor(x => x.Zone)
#Html.HiddenFor(x => x.Zone)
</td>
<td>
#Html.DisplayFor(x => x.ZoneDescription)
#Html.HiddenFor(x => x.ZoneDescription)
</td>
<td>
#Html.DisplayFor(x => x.Rack)
#Html.HiddenFor(x => x.Rack)
</td>
<td>
#Html.DisplayFor(x => x.ActualProduct)
#Html.HiddenFor(x => x.ActualProduct)
</td>
<td>
#Html.DisplayFor(x => x.Cost)
#Html.HiddenFor(x => x.Cost)
</td>
<td>
#Html.TextBoxFor(x => x.DMarkup)
</td>
<td>
#Html.TextBoxFor(x => x.DSell)
</td>
<td>
#Html.TextBoxFor(x => x.RMarkup)
</td>
<td>
#Html.TextBoxFor(x => x.RSell)
</td>
<td>
#Html.DisplayFor(x => x.DateUpdated)
#Html.HiddenFor(x => x.DateUpdated)
</td>
<td>
<button type="submit">Update</button>
</td>
</tr>
}
Controllers
After updating in database, you cannot return View("Index", model);. Index view is expecting an enumerable. The best approach is to redirect to Index page again.
public class HomeController : Controller
{
public ActionResult Index()
{
List<OPISPriceReportOLY_Result> results = new List<OPISPriceReportOLY_Result>();
results.Add(new OPISPriceReportOLY_Result { cpid = 1 });
results.Add(new OPISPriceReportOLY_Result { cpid = 2 });
results.Add(new OPISPriceReportOLY_Result { cpid = 3 });
return View(results);
}
[HttpPost]
public ActionResult Update(OPISPriceReportOLY_Result model)
{
if (ModelState.IsValid)
{
int id = model.orpid;
using (var context = new IntranetCoreEntities())
{
var selected = context.OPISRetailPricings.Find(id);
selected.DMarkup = model.DMarkup;
selected.DSell = model.DSell;
selected.RMarkup = model.RMarkup;
selected.RSell = model.RSell;
context.SaveChanges();
}
}
return RedirectToAction("Index");
}
}

Related

The model item passed into the dictionary is of type .. but this dictionary requires a model item of type System.Collections.Generic.IEnumerable'

How to solve this error ?
The model item passed into the dictionary is of type 'System.Data.EnumerableRowCollection1[System.Data.DataRow]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[AljawdahNewSite.Models.LAB_INVOICE_VIEW]'.
1- This is the Model :
public partial class LAB_INVOICE_VIEW
{
public int patient_no { get; set; }
public int order_id { get; set; }
public string patient_name { get; set; }
public int testid { get; set; }
public string testname { get; set; }
public string order_vat { get; set; }
public Nullable<decimal> total_amount { get; set; }
public string status_name { get; set; }
public Nullable<System.DateTime> COLLECTION_DATE { get; set; }
public Nullable<System.DateTime> RECEIVING_DATE { get; set; }
}
2- This is the controller :
public ActionResult Index(int id)
{
string sql = #"select patient_no ,
order_id ,
patient_name ,
testid ,
testname ,
order_vat ,
total_amount ,
status_name ,
COLLECTION_DATE ,
RECEIVING_DATE
FROM lab_invoice_view
where ORDER_ID = '{0}' ";
DataTable dt = func.fireDatatable(string.Format(sql, id));
LAB_INVOICE_VIEW invoice = new LAB_INVOICE_VIEW();
foreach (DataRow dr in dt.Rows)
{
invoice.patient_no = int.Parse(dr["patient_no"].ToString());
invoice.order_id = int.Parse(dr["order_id"].ToString());
invoice.patient_name = dr["patient_name"].ToString();
invoice.testid = int.Parse(dr["testid"].ToString());
invoice.testname = dr["testname"].ToString();
invoice.order_vat = dr["order_vat"].ToString();
invoice.total_amount = decimal.Parse(dr["total_amount"].ToString());
invoice.status_name = dr["status_name"].ToString();
invoice.COLLECTION_DATE = DateTime.Parse(dr["COLLECTION_DATE"].ToString());
invoice.RECEIVING_DATE = DateTime.Parse(dr["RECEIVING_DATE"].ToString());
}
return View(dt.AsEnumerable());
}
3- This is the view :
#model IEnumerable<AljawdahNewSite.Models.LAB_INVOICE_VIEW>
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_LayoutMain.cshtml";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.patient_no)
</th>
<th>
#Html.DisplayNameFor(model => model.order_id)
</th>
<th>
#Html.DisplayNameFor(model => model.patient_name)
</th>
<th>
#Html.DisplayNameFor(model => model.testid)
</th>
<th>
#Html.DisplayNameFor(model => model.testname)
</th>
<th>
#Html.DisplayNameFor(model => model.order_vat)
</th>
<th>
#Html.DisplayNameFor(model => model.total_amount)
</th>
<th>
#Html.DisplayNameFor(model => model.status_name)
</th>
<th>
#Html.DisplayNameFor(model => model.COLLECTION_DATE)
</th>
<th>
#Html.DisplayNameFor(model => model.RECEIVING_DATE)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.patient_no)
</td>
<td>
#Html.DisplayFor(modelItem => item.order_id)
</td>
<td>
#Html.DisplayFor(modelItem => item.patient_name)
</td>
<td>
#Html.DisplayFor(modelItem => item.testid)
</td>
<td>
#Html.DisplayFor(modelItem => item.testname)
</td>
<td>
#Html.DisplayFor(modelItem => item.order_vat)
</td>
<td>
#Html.DisplayFor(modelItem => item.total_amount)
</td>
<td>
#Html.DisplayFor(modelItem => item.status_name)
</td>
<td>
#Html.DisplayFor(modelItem => item.COLLECTION_DATE)
</td>
<td>
#Html.DisplayFor(modelItem => item.RECEIVING_DATE)
</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>
4- this is the actionlink when click the link :
<td>#Html.ActionLink("E-Invoice", "Index", "Invoice", new { id = item.LabOrders.ORDER_ID}, new { #class = "btn btn-primary", target = "_blank" })</td>
what I need to change in the code ?
I changed the way and used the dbcontext as the follwing code its solved my issue :
public ActionResult Index(int id)
{
var Invoice = db.LAB_INVOICE_VIEW.Where(c => c.order_id == id);
return View(Invoice);
}

how to format a date time variable in asp.net mvc using #HTML.DisplayNameFor

I have this statement in my view that as follows:
<th>
#Html.DisplayNameFor(model => model.ReleaseDate)
</th>
all I'm trying to do is format the release date so that it will be in the format of mm/dd/yyyy. I've searched loads and haven't come across anything that is exactly what I'm looking for. Any help would be greatly appreciated, I'm relatively new to coding.
Here is my Model:
using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
namespace MvcMovie2.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public string Rating { get; set; }
public decimal Price { get; set; }
}
public class MovieDBContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
public class DummyModel : Movie
{
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime ReleaseDate { get; set; }
}
}
and here is my view:
#model IEnumerable<MvcMovie2.Models.Movie>
#{
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.DisplayFor(model => model.ReleaseDate)
</th>
<th>
#Html.DisplayNameFor(model => model.Genre)
</th>
<th>
#Html.DisplayNameFor(model => model.Rating)
</th>
<th>
#Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
#Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
#Html.DisplayFor(modelItem => item.Price)
</td>
<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>
You can achieve that with the DisplayFormatAttribute :
using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
namespace MvcMovie2.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public string Rating { get; set; }
public decimal Price { get; set; }
}
public class MovieDBContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
}
Your view is strongly typed to IEnumerable<MvcMovie2.Models.Movie> therefore you cannot use the html helper like you did. Instead separate your code in partial views that are strongly type to MvcMovie2.Modes.Movie :
Code for the actual view :
#model IEnumerable<MvcMovie2.Models.Movie>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
#Html.Partial("Header")
#foreach (var item in Model)
{
#Html.Partial("Movie", item)
}
</table>
Code for the Header.cshtml partial view :
#model MvcMovie2.Models.Movie
<tr>
<th>
#Html.DisplayNameFor(model => model.Title)
</th>
<th>
#Html.DisplayNameFor(model => model.ReleaseDate)
</th>
<th>
#Html.DisplayNameFor(model => model.Genre)
</th>
<th>
#Html.DisplayNameFor(model => model.Rating)
</th>
<th>
#Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
Code for the Movie.cshtml partial view :
#model MvcMovie2.Models.Movie
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
#Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
#Html.DisplayFor(modelItem => item.Price)
</td>
<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>
I got it to work. All I had to do was put this using clause in my model:
using System.ComponentModel.DataAnnotations;
then I put this line of code above my code that defined my Release Date:
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
and then it worked perfectly, I didn't have to change anything in the view.

How to total up a column of a database and display it through a label in asp.net mvc?

I would like to calculate the total of a db column (Amount) and display it on a label next to the total label in the view. I am unsure how to do carry out this particular task, should it be done in Javascript? or what other methods can I use?
This is my project...
MODEL
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace webassignment.Models
{
public class Donation
{
public int ID { get; set; }
public string DisplayName{ get; set; }
public DateTime Date { get; set; }
public decimal Amount { get; set; }
public decimal TaxBonus { get; set; }
public string Comment { get; set; }
}
public class DonationDBContext: DbContext
{
public DbSet<Donation> Donation { get; set; }
}
}
INDEX
// GET: Donations
public ActionResult Index()
{
return View(db.Donation.ToList());
}
INDEX VIEW
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.DisplayName)
</th>
<th>
#Html.DisplayNameFor(model => model.Date)
</th>
<th>
#Html.DisplayNameFor(model => model.Amount)
</th>
<th>
#Html.DisplayNameFor(model => model.TaxBonus)
</th>
<th>
#Html.DisplayNameFor(model => model.Comment)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.DisplayName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Date)
</td>
<td>
#Html.DisplayFor(modelItem => item.Amount)
</td>
<td>
#Html.DisplayFor(modelItem => item.TaxBonus)
</td>
<td>
#Html.DisplayFor(modelItem => item.Comment)
</td>
<td>
</td>
<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>
<script type="text/javascript">
You can do that in View, but better way would be to create a ViewModel, i am giving you basic idea how you can do it in View for time being :
#{
int totalAmount;
if(Model !=null)
{
totalAmount = Model.Sum(x=>x.Amount);
}
}
and down display it in html where ever you want:
<h1>#totalAmount</h1>
It appears that you are passing to the razor view a List of Donations so you should be able to use the extension methods for collections like model.Sum(x=> x.Amount) within your display.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`

Model: StudentData (Model 1)
namespace Aug16.Models
{
[Table("Stdnt_Info")]
public class StudentData
{
[Key]
public long Stdnt_Id { get; set; }
public string Stdnt_Name { get; set; }
public string Stdnt_Fname { get; set; }
public string Stdnt_Address { get; set; }
public string Stdnt_Semmester { get; set; }
public DateTime Sem_StartDate { get; set; }
public DateTime Sem_EndDate { get; set; }
public int Stdnt_Mark1 { get; set; }
public int Stdnt_Mark2 { get; set; }
public int Stdnt_Mark3 { get; set; }
public Decimal Stdnt_Sem_Per { get; set; }
}
}
-------------------------------------------------------------------------------
DBContext Model: Aug16Data (Model2)
namespace Aug16.Models
{
public class Stdnt_Details : DbContext
{
public DbSet<StudentData> Student_Information { get; set; }
//public DbSet<Stdnt_Details> Student_Information { get; set; }
//public DbSet<Aug16Data> Stdnt_Mark { get; set; }
}
}
------------------------------------------------------------------------------
Controller : StudentDataController
namespace Aug16.Controllers
{
public class StudentDataController : Controller
{
//
// GET: /StudentData/
Aug16.Models.Stdnt_Details StoreDB = new Models.Stdnt_Details();
public ActionResult Aug16DataAction()
{
var StdDet = StoreDB.Student_Information.ToList();
return View(StdDet);
}
}
}
---------------------------------------------------------------------------
View: Aug16DataAction
#model IEnumerable<Aug16.Models.Stdnt_Details>
#{
ViewBag.Title = "Aug16DataAction";
}
<h2>Aug16DataAction</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<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>
Error as below
Server Error in '/' Application.
The model item passed into the dictionary is of type 'System.Collections.Generic.List[Aug16.Models.StudentData]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable[Aug16.Models.Stdnt_Details]'.
Please what is the solution for this error?
First, you need to change your model type, as mentioned on comments to your question.
However, you are looking an empty view because you don't have the code to show all the model properties. Try to change your view with this code:
#model IEnumerable<Test.Models.StudentData>
#{
ViewBag.Title = "Aug16DataAction";
}
<h2>Aug16DataAction</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Stdnt_Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Stdnt_Fname)
</th>
<th>
#Html.DisplayNameFor(model => model.Stdnt_Address)
</th>
<th>
#Html.DisplayNameFor(model => model.Stdnt_Semmester)
</th>
<th>
#Html.DisplayNameFor(model => model.Sem_StartDate)
</th>
<th>
#Html.DisplayNameFor(model => model.Sem_EndDate)
</th>
<th>
#Html.DisplayNameFor(model => model.Stdnt_Mark1)
</th>
<th>
#Html.DisplayNameFor(model => model.Stdnt_Mark2)
</th>
<th>
#Html.DisplayNameFor(model => model.Stdnt_Mark3)
</th>
<th>
#Html.DisplayNameFor(model => model.Stdnt_Sem_Per)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Stdnt_Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Stdnt_Fname)
</td>
<td>
#Html.DisplayFor(modelItem => item.Stdnt_Address)
</td>
<td>
#Html.DisplayFor(modelItem => item.Stdnt_Semmester)
</td>
<td>
#Html.DisplayFor(modelItem => item.Sem_StartDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Sem_EndDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Stdnt_Mark1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Stdnt_Mark2)
</td>
<td>
#Html.DisplayFor(modelItem => item.Stdnt_Mark3)
</td>
<td>
#Html.DisplayFor(modelItem => item.Stdnt_Sem_Per)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.Stdnt_Id }) |
#Html.ActionLink("Details", "Details", new { id = item.Stdnt_Id }) |
#Html.ActionLink("Delete", "Delete", new { id = item.Stdnt_Id })
</td>
</tr>
}
</table>

Populate form from table select with asp.net mvc

I have a table with data, how can I populate a form on the same page with the data when the edit button is clicked. Basically is should be the same as this example but without using knockoutjs
http://jsfiddle.net/jiggle/2cr2f/
#model IEnumerable<GenomindApp2.Areas.RulesEngine.ViewModels.GeneViewModel>
#{
ViewBag.Title = "Index2";
}
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.GeneValue)
</th>
<th>
#Html.DisplayNameFor(model => model.GeneCode)
</th>
<th>
#Html.DisplayNameFor(model => model.GeneName)
</th>
<th>
#Html.DisplayNameFor(model => model.GeneComments)
</th>
<th>
#Html.DisplayNameFor(model => model.WildType)
</th>
<th>
#Html.DisplayNameFor(model => model.WildTypeAllele)
</th>
<th>
#Html.DisplayNameFor(model => model.AtRiskAllele)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.GeneCode)
</td>
<td>
#Html.DisplayFor(modelItem => item.GeneName)
</td>
<td>
#Html.DisplayFor(modelItem => item.GeneComments)
</td>
<td>
#Html.DisplayFor(modelItem => item.WildType)
</td>
<td>
#Html.DisplayFor(modelItem => item.WildTypeAllele)
</td>
<td>
#Html.DisplayFor(modelItem => item.AtRiskAllele)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { }) |
#Html.ActionLink("Details", "Details", new { }) |
#Html.ActionLink("Delete", "Delete", new { })
</td>
</tr>
}
</table>
You should do it like that:
Model:
public class ModelB
{
public int Age { get; set; }
public string Name { get; set; }
}
Controller:
public ActionResult MyAction()
{
var model = new List<ModelB>
{
new ModelB{Age = 2, Name = "Bob"},
new ModelB{Age = 7, Name = "Sam"},
};
return View(model);
}
[HttpPost]
public ActionResult MyAction(List<ModelB> model)
{
//whatever
}
View:
#model List<TestWebApplication.Models.ModelB>
...
#using (Html.BeginForm())
{
for (int i = 0; i < Model.Count; i++)
{
Age: #Html.EditorFor(modelItem => Model[i].Age);
Name: #Html.EditorFor(modelItem => Model[i].Name);
<br />
}
<input type="submit"/>
}
Please note that I used for instead of foreach. When you populate a form you shouldn't use foreach - it will not render well.

Categories

Resources