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.
Related
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);
}
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");
}
}
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>
I'm trying to submit only these rows which is checked. I'm using this jquery plungin and my table looks same as is in link
#model IEnumerable<BillBox.ACD_UNI_STUDENTS>
<table class="table tblSelect">
<tr>
<th>
<input type="checkbox" id="checkall" title="Select all" />
</th>
<th>
#Html.DisplayNameFor(model => model.FIRST_NAME)
</th>
<th>
#Html.DisplayNameFor(model => model.LAST_NAME)
</th>
<th>
#Html.DisplayNameFor(model => model.PERSONAL_NUMBER)
</th>
<th>
#Html.DisplayNameFor(model => model.ACD_UNI_DEGREES.DEGREE)
</th>
<th>
#Html.DisplayNameFor(model => model.ACD_UNI_FACULTIES.FACULTY)
</th>
<th>
#Html.DisplayNameFor(model => model.ACD_UNI_SEMESTERS.SEMESTER)
</th>
<th>
#Html.DisplayNameFor(model => model.ACD_UNI_SPECIALIZATIONS.SPECIALIZATION)
</th>
<th>
#Html.DisplayNameFor(model => model.COR_PAYER_STATUS.NAME)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
<input type="checkbox"/>
</td>
<td>
#Html.DisplayFor(modelItem => item.FIRST_NAME)
</td>
<td>
#Html.DisplayFor(modelItem => item.LAST_NAME)
</td>
<td>
#Html.DisplayFor(modelItem => item.PERSONAL_NUMBER)
</td>
<td>
#Html.DisplayFor(modelItem => item.ACD_UNI_FACULTIES.FACULTY)
</td>
<td>
#Html.DisplayFor(modelItem => item.ACD_UNI_SEMESTERS.SEMESTER)
</td>
<td>
#Html.DisplayFor(modelItem => item.ACD_UNI_SEMESTERS.SEMESTER)
</td>
<td>
#Html.DisplayFor(modelItem => item.ACD_UNI_SPECIALIZATIONS.SPECIALIZATION)
</td>
<td>
#Html.DisplayFor(modelItem => item.COR_PAYER_STATUS.NAME)
</td>
</tr>
}
</table>
Now I have a question. How can I retrieve only these rows which checkbox is checked?
that's my controller
public PartialViewResult AllStudent()
{
var students = (from q in db.ACD_UNI_STUDENTS
select q).ToList();
return PartialView(students);
}
there are many rows (from db) so I can't do it with formcollection. I can't retrieve their names
You may submit the selected ids as IEnumerable and then filter them in your controller! Here is an example
<form action="url" method="post">
...
#foreach (var item in Model)
{
<tr>
<td>
<input type="checkbox" name="Objs[]" id="Objs[]" value="#item.UNIQUE_ID"/>
</td>
<td>
#Html.DisplayFor(modelItem => item.FIRST_NAME)
</td>
</tr>
}...
</form>
and your controller
[HttpPost]
public PartialViewResult AllStudent(IEnumerable<long> Objs)
{
var students = (from q in db.ACD_UNI_STUDENTS
where Objs.Contains(q.UNIQUE_ID)
select q).ToList();
return PartialView(students);
}
...
I'd suggest not to use the Database Model for the View directly. You should have a Model to bind to the Rows, which might look like:
public class StudentRowViewModel
{
public bool IsChecked { get; set; }
public int StudentId { get; set; }
// ... More columns, whatever you want to display
public string Name { get; set; }
}
When populating the View, you select StudentRowViewModels like this:
db.ACD_UNI_STUDENTS.Select(x => new StudentRowViewModel { StudentId = x.UNIQUE_ID, Name = ... });
Or whatever applies to your DataBase Model.
In your View, your Checkbox will also Bind to the Model:
#Html.CheckboxFor(x => x.IsChecked)
Finally, when the Form is submitted, you can select only the checked Items:
public ActionResult AllStudents(IEnumerable<StudentRowViewModel> model)
{
var checked = model.Where(x => x.IsChecked).Select(x => x.StudentId).ToList();
var items = db.ACD_UNI_STUDENTS.Where(x => checked.Contains(x.UNIQUE_ID));
}
You get the Idea?
First put a class and value attribute in your checkbox :
<input type="checkbox" class="myCheckBox" value="#item.PERSONAL_NUMBER" />
Then i advise you to use a JQuery script to detect which checkbox are checked :
I suppose that you have a button to do another action :
$(document).ready(function () {
$('#myButton').click(function() {
var id = '';
$('.myCheckBox:checked').each(function (e) {
id += $(this).val() + ';'
}
var url = '/ControllerName/ActionResultName';
$.ajax({
url:url,
cache: false,
type: 'POST',
data: {
Id: id
},
succes: function(data){
$('.myCheckBox').attr('checked',false);
location.reload();
}
})
}
});
After that you can get in your controller all the personnal numbers in your controller :
put a string parameter 'id' to your actionresult.
id.TrimEnd(';')
Problem:
I have List of Categories with SubCategories, so when i click on SubCategory item it redirect me to List of Entries. There along the list it has Create ActionLink. So the problem is passing the SubCategoryId when I click SubCategoryItem for the Create ActionLink. Without the CreateActionLink it lists the entries but with it, it gives an error in the index view:
Object reference not set to an instance of an object.
Line 6:
Line 7: <p>
Line 8: #Html.ActionLink("Create New", "Create", new { subCategoryId = #Model.SubCategoryId})
Line 9: </p>
Line 10:
I understand that i am passing null reference and the question is how to avoid that?
Here is my code:
Controller:
public class EntryController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult EntryList(int subCategoryId)
{
var entries = EntryDAL.GetEntries(subCategoryId);
return View("_EntryList",entries);
}
public ActionResult Create(int subCategoryId)
{
var model = new Entry();
model.SubCategoryId = subCategoryId;
return View(model);
}
[HttpPost]
public ActionResult Create(Entry entry)
{
try
{
if (ModelState.IsValid)
{
var add = EntryDAL.Add(entry);
return RedirectToAction("Index");
}
return View(entry);
}
catch (Exception)
{
return View();
}
}
}
IndexView:
#model PasswordCloud.Domain.Models.SubCategory
#{
ViewBag.Title = "Index";
}
<p>
#Html.ActionLink("Create New", "Create", new { subCategoryId = #Model.SubCategoryId })
</p>
#{Html.RenderPartial("_EntryList",Model.EntryList);}
PartialView:
#model IEnumerable<PasswordCloud.Domain.Models.Entry>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Title)
</th>
<th>
#Html.DisplayNameFor(model => model.Username)
</th>
<th>
#Html.DisplayNameFor(model => model.Password)
</th>
<th>
#Html.DisplayNameFor(model => model.Url)
</th>
<th>
#Html.DisplayNameFor(model => model.Description)
</th>
<th>
#Html.DisplayNameFor(model => model.SubCategoryId)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Username)
</td>
<td>
#Html.DisplayFor(modelItem => item.Password)
</td>
<td>
#Html.DisplayFor(modelItem => item.Url)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.DisplayFor(modelItem => item.SubCategoryId)
</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>
SubCategoryListItem View:
#model IEnumerable<PasswordCloud.Domain.Models.SubCategory>
#foreach (var item in Model) {
#Html.ActionLink(item.Name,"Index","Entry", new { subCategoryId = item.SubCategoryId }, null)
}
In your Index action you never create a model and pass it to the view. So when it gets to your line where you make the action link where you use new { subCategoryId = #Model.SubCategoryId} your model is null. Thus you get a null ref exception. To fix it you need to do something like this.
public ActionResult Index()
{
var model = ...
return View(model);
}