I have a scenario where in I need to store multiple rows in a single table.Let me explain it in detail.
I have a table Price which has 4 columns, ID, ModelID, AppSettingID,Amount.
I am looking for inserting multiple values to the table where
ID would be the Primary Key.
ModelID would be same for all the rows.
AppSettingID and Amount will be different for all the rows and would be based on the selection user does on the view.
I have bound the AppSettingID to different combo boxes on the view as I have it categorized in the database.
This is what I am doing right now.
View:
<div class="editor-label">
#Html.LabelFor(model => model.ModelID, "Model")
</div>
<div class="editor-field">
#Html.DropDownList("ModelID", String.Empty)
#Html.ValidationMessageFor(model => model.ModelID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.AppsettingID, "Mobile Condition")
</div>
<div class="editor-field">
#Html.DropDownList("Mobile Condition", new SelectList(ViewBag.ConditionID, "Text", "Value"))
#Html.ValidationMessageFor(model => model.AppsettingID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Amount)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Amount)
#Html.ValidationMessageFor(model => model.Amount)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.AppsettingID, "Tennure")
</div>
<div class="editor-field">
#Html.DropDownList("Tennure", new SelectList(ViewBag.AppsettingID, "TexT", "Value"))
#Html.ValidationMessageFor(model => model.AppsettingID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Amount)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Amount)
#Html.ValidationMessageFor(model => model.Amount)
</div>
<p>
<input type="submit" value="Create" />
</p>
Controller:
public ActionResult Create()
{
//ViewBag.AppsettingID = db.Appsettings.Select(r => r.ID).Distinct();
ViewBag.ModelID = new SelectList(db.Models, "ID", "Name");
//ViewBag.Tennure = db.Appsettings.Select(s => s.Type == "Tennure").Distinct();
IQueryable<Appsetting>TennureIDs = db.Appsettings.Where(s => s.Type == "Tennure").Distinct();
IQueryable<Appsetting> Conditions = db.Appsettings.Where(s => s.Type == "Mobile Condition").Distinct();
List<SelectListItem> items = new List<SelectListItem>();
foreach (var t in TennureIDs)
{
SelectListItem s = new SelectListItem();
s.Text = t.ID.ToString();
s.Value = t.Value.ToString();
items.Add(s);
}
ViewBag.AppsettingID = items;
List<SelectListItem> conds = new List<SelectListItem>();
foreach (var t in Conditions)
{
SelectListItem s = new SelectListItem();
s.Text = t.ID.ToString();
s.Value = t.Value.ToString();
conds.Add(s);
}
ViewBag.ConditionID = conds;
return View();
}
//
// POST: /Price/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Price price, FormCollection form)
{
if (ModelState.IsValid)
{
int test = Convert.ToInt16(form["Mobile Condition"]);
price.AppsettingID = test;
db.Prices.Add(price);
db.SaveChanges();
return RedirectToAction("Index");
}
//ViewBag.AppsettingID = new SelectList(db.Appsettings, "ID", "Type", price.AppsettingID);
//ViewBag.ModelID = new SelectList(db.Models, "ID", "Name", price.ModelID);
return View(price);
}
I hope this solution will help you
The following is a razor page, here I have displayed the model in table format
#model List<MyModel>
<h2>Create</h2>
#using (Html.BeginForm())
{
<table>
<tr>
<th>
Model ID
</th>
<th>
App Setting ID
</th>
<th>
Amount
</th>
</tr>
#for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
#Html.TextBoxFor(x => Model[i].ModelID)
</td>
<td>
#Html.TextBoxFor(x => Model[i].AppsettingID)
</td>
<td>
#Html.TextBoxFor(x => Model[i].Amount)
</td>
</tr>
}
</table>
<input type="submit" />
}
When user clicks the submit button, it will pass the model to the controller
Controller
[HttpPost]
public ActionResult Create(List<MyModel> m)
{
// Do Coding
return View("Create", m);
}
Related
I have a search screen which gives the results in a paged list. On changing the page I need to get the values of the Model to the GET method in the controller. While I was able to pass the model properties which are strings, I am having an issue passing the string list.
View Code :
<div class="divSearch">
<div class="divCriteria">
<div class="row">
<div class="col-md-6">
#Html.LabelFor(m => m.Name)
#Html.TextBoxFor(m => m.Name, new { #class = "form-control" })
</div>
<div class="col-md-6">
#Html.LabelFor(m => m.Owner)
#Html.TextBoxFor(m => m.Owner, new { #class = "form-control" })
</div>
</div>
<br />
<div class="row">
<div class="col-md-6">
#Html.LabelFor(m => m.County)
#Html.ListBoxFor(model => model.County, Model.CountiesList, new { #class = "form-control", multiple = "multiple" })
</div>
</div>
<br />
<div class="row">
<div class="right">
<button type="submit" class="btn btn-primary"><i class="fa fa-share-square-o"></i>Search</button>
</div>
</div>
</div>
<div class="divResults">
<div class="table-responsive">
<table class="table table-hover table-advance dataTable">
<thead>
<tr>
<th style="display:none">ID</th>
<th>Name</th>
<th>Type</th>
<th>County</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.SearchList)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
#Html.HiddenFor(modelItem => item.ID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Type)
</td>
<td>
#Html.DisplayFor(modelItem => item.County)
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
#if (Model.SearchList != null)
{
var county = new List<string>();
foreach (var item in Model.County)
{
county.Add(item);
}
#Html.PagedListPager(Model.SearchList, Page => Url.Action("Index", "FacilityFinder", new RouteValueDictionary() { { "Page", Page }, { "name", Model.Name }, { "owner", Model.Owner }, { "county", county} }),PagedListRenderOptions.PageNumbersOnly)
}
Controller code :
public ActionResult Index(int? page=null,string name = null, List<string> county=null,string owner = null)
{
}
The value for name and owner are fine in the controller, but the list of county gives me System.Collections.Generic.List`1[System.String]
Am I missing something?
You can't pass complex types such as lists. You might need to construct your RouteValueDictionary dynamically:
var query = new RouteValueDictionary
{
{ "name", Model.Name },
{ "owner", Model.Owner }
};
for (var i = 0; i < Model.County.Count; i++)
{
query["county[" + i + "]"] = Model.County[i];
}
#Html.PagedListPager(
Model.SearchList,
Page => Url.Action("Index", "FacilityFinder", new RouteValueDictionary(query) { { "Page", Page } }),
PagedListRenderOptions.PageNumbersOnly
)
so that the resulting url looks like this:
/FacilityFinder/Index?Page=5&name=Foo&owner=Bar&county[0]=foo1&county[1]=foo2...
which will make the default model binder in ASP.NET MVC happy and properly bind this to a List<string>.
Here is my Create Method
/* [HttpPost]
public ActionResult Create(Image newRecipe)
{
try
{
using (var db = new MitishaKitchenContext())
{
db.Images.Add(newRecipe);
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch { return View(); }
}*/
[HttpPost]
public ActionResult Create(Image IG)
{
// Apply Validation Here
if (IG == null)
{
return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
}
if (IG != null && IG.File.ContentLength > (2 * 1024 * 1024))
{
ModelState.AddModelError("CustomError", "File size must be less than 2 MB");
return View();
}
if (!(IG.File.ContentType == "image/jpeg" || IG.File.ContentType == "image/gif"))
{
ModelState.AddModelError("CustomError", "File type allowed : jpeg and gif");
return View();
}
// IG.FileName = IG.File.FileName;
// IG.ImageSize = IG.File.ContentLength;
byte[] data = new byte[IG.File.ContentLength];
IG.File.InputStream.Read(data, 0, IG.File.ContentLength);
IG.ImageData = data;
using (MitishaKitchenContext dc = new MitishaKitchenContext())
{
dc.Images.Add(IG);
dc.SaveChanges();
}
return RedirectToAction("Index");
}
}
How to create EDIT method and EDIT Razor View for Image which i stored in database in VARBINARY datatype in ASP.net MVC?
Here is my Create View:
{
#using (Html.BeginForm("Create","Recipes",null, FormMethod.Post,new {enctype="multipart/form-data"}))
{
#Html.ValidationSummary(true)
<fieldset>
<legend>Image</legend>
<div class="editor-label">
#Html.LabelFor(model => model.ImageName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ImageName)
#Html.ValidationMessageFor(model => model.ImageName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Recipe.RecipeName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Recipe.RecipeName)
#Html.ValidationMessageFor(model => model.Recipe.RecipeName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Recipe.RecipeDescriptions)
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.Recipe.RecipeDescriptions,new {style = "width:90%;height:400px"})
#Html.ValidationMessageFor(model => model.Recipe.RecipeDescriptions)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Recipe.RecipeIngredients)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Recipe.RecipeIngredients)
#Html.ValidationMessageFor(model => model.Recipe.RecipeIngredients)
</div>
<br />
<table>
<tr>
<td>Select File : </td>
<td>
#Html.TextBoxFor(Model=> Model.File, new{type="file"})
#Html.ValidationMessage("CustomError")
</td>
<td>
<input type="submit" value="Upload" />
</td>
</tr>
</table>
enter code here
</fieldset>
}
}
You would need to have the following components in your database table:
RecordId, ImageName, RecipeName, RecipeDescriptions, RecipeDescriptions, ImageData.
Then in the edit you can do something like this:
[HttpGet]
public ActionResult Edit(int recordId)
{
using (var db = new MitishaKitchenContext())
{
// fetch data for the image by Id
var yourmodel = db.Images.Where(x => x.RecordId == recordId).FirstOrDefault();
if(yourmodel != null)
{
return View("Create", yourmodel);
}
}
return RedirectToAction("Index");
}
You can use the same view as the Create uses as the model will populate your fields. And then you should search if the recordId exists and upsert(insert or update if it already exists).
when i will select check box and will be clicked select button how i can get this value to the textbox value. Here checkbox id= chk and textbox id = OrderNo. Please help me ....I have something in below....
$("#OrderNo").blur(function() {
$.get('/Ordering/OrderList/', function(data) {
$('#orlist').html(data);
});
$('#orlist').dialog({
width: 500,
height: 350,
open: true,
title: 'Select Order',
buttons: {
Select: function() {
if (("#chk") == 'checked') {
var por = ("#porderno").val();
por = ("#OrderNo");
}
},
Cancel: function() {
$('#orlist').dialog('close');
$('#orlist').empty();
}
}
});
Partial view page is
#model IEnumerable<testcon.OrderM>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.OdrId)
</th>
<th>
#Html.DisplayNameFor(model => model.OrderNo)
</th>
<th>
#Html.DisplayNameFor(model => model.CId)
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.CheckBox("OdrId", new { #id="chk"})
</td>
<td class="left">
#Html.DisplayFor(modelItem => item.OrderNo, new { #id="porderno"})
</td>
<td class="left">
#Html.DisplayFor(modelItem => item.CId)
</td>
</tr>
}
</table>
And My Create View page is
#model testcon.DeliveryInfo
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>DeliveryInfo</legend>
#*<div class="editor-label">
#Html.LabelFor(model => model.DId)
</div>*#
<div class="editor-field">
Delivery Id : #Html.EditorFor(model => model.DId)
#Html.ValidationMessageFor(model => model.DId)
</div>
#*<div class="editor-label">
#Html.LabelFor(model => model.OrderNo)
</div>*#
<div class="editor-field">
Order No :#Html.EditorFor(model => model.OrderNo)
#Html.ValidationMessageFor(model => model.OrderNo)
#*Show Order*#
</div>
#* <div class="editor-label">
#Html.LabelFor(model => model.DDate)
</div>*#
<div class="editor-field">
Delivery Date : #Html.EditorFor(model => model.DDate)
#Html.ValidationMessageFor(model => model.DDate)
</div>
#*<div class="editor-label">
#Html.LabelFor(model => model.DQuantity)
</div>*#
<div class="editor-field">
Delivery Quantity: #Html.EditorFor(model => model.DQuantity)
#Html.ValidationMessageFor(model => model.DQuantity)
</div>
#*<div class="editor-label">
#Html.LabelFor(model => model.DAmount)
</div>*#
<div class="editor-field">
Delivery Amount : #Html.EditorFor(model => model.DAmount)
#Html.ValidationMessageFor(model => model.DAmount)
</div>
<div id="orlist" >
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
Use .is(':checked') check if checked...
buttons: {
Select: function() {
if ($("#chk").is(':checked')) {
$("#OrderNo").val($("#porderno").val());
}
},
to check if the check box is checked in jQuery, you can do it by either
//will return true if the check box is checked otherwise false!
$("#chkBoxId").is(':checked');
or
this approach is good when dealing with group of radio buttons/checkboxes.
//will yield the same result if checkbox is checked
$('input[name="chkBoxName"]:checked').val();
So you can use them in your code like what #Anthony Chu has responded which i was about to write :)
I am getting this error when trying to load a partial view, and I don't know what the issue is:
System.InvalidOperationException: The model item passed into the
dictionary is of type 'CDB.OrderM', but this dictionary requires a
model item of type
'System.Collections.Generic.IEnumerable`1[CDB.tblItem]'.
public ActionResult Index()
{
OrderM om = new OrderM();
List<tblItem> tList = db.Query<tblItem>("Select * from tblItem").ToList<tblItem>();
ViewBag.tList = tList;
return View(om);
}
public ActionResult Reqitem()
{
//tblItem ti= db.Query<tblItem>("select * from tblItem");
var ti = db.Query<tblItem>("select * from tblItem");
return PartialView("_rawmat",ti);
}
My Partial View codes are-
#model IEnumerable<CDB.tblItem>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.ItemId)
</th>
<th>
#Html.DisplayNameFor(model => model.ItemName)
</th>
<th>
#Html.DisplayNameFor(model => model.MeasuringUnit)
</th>
<th>
#Html.DisplayNameFor(model => model.Rate)
</th>
<th>
#Html.DisplayNameFor(model => model.Quantity)
</th>
<th>
#Html.DisplayNameFor(model => model.BagSz)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.ItemId)
</td>
<td>
#Html.DisplayFor(modelItem => item.ItemName)
</td>
<td>
#Html.DisplayFor(modelItem => item.MeasuringUnit)
</td>
<td>
#Html.DisplayFor(modelItem => item.Rate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
#Html.DisplayFor(modelItem => item.BagSz)
</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>
and Index view codes are...
#model CDB.OrderM
#{
ViewBag.Title = "Index";
var tList = ViewBag.tList;
}
<h2>Index</h2>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<fieldset>
<legend>OrderM</legend>
<div class="editor-label">
#Html.LabelFor(model => model.OdrId)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.OdrId)
#Html.ValidationMessageFor(model => model.OdrId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.OrderNo)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.OrderNo)
#Html.ValidationMessageFor(model => model.OrderNo)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.OdrDate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.OdrDate)
#Html.ValidationMessageFor(model => model.OdrDate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.OdrQty)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.OdrQty)
#Html.ValidationMessageFor(model => model.OdrQty)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.OdrAmount)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.OdrAmount)
#Html.ValidationMessageFor(model => model.OdrAmount)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.DDate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.DDate)
#Html.ValidationMessageFor(model => model.DDate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.CId)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.CId)
#Html.ValidationMessageFor(model => model.CId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Pod)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Pod)
#Html.ValidationMessageFor(model => model.Pod)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
#Html.Partial("_rawmat")
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
It looks like what you want to do is change #Html.Partial("_rawmat") to #Html.Action("Reqitem"). This is because your original statement says go straight to the view and since you haven't passed a model it will pass the same model as the view that it is included on.
So if you use #Html.Partial("_rawmat") on your index view it will pass the model that it has which is of type OrderM and not actually call the action you have written at all.
I dont know why you people are missing out the available syntax to call a partial view with a model
{#Html.RenderPartial("_PartialView",List<CDB.tblItem>) }
Where I assume the partial view uses the Model of type List<CDB.tblItem>.
If you want to populate the model with any values. just before the above syntax use
#{
//code to populate your model
}
If your partial view is expecting a List, then change the method like this...
public ActionResult Reqitem()
{
//tblItem ti= db.Query<tblItem>("select * from tblItem");
var ti = db.Query<tblItem>("select * from tblItem").ToList();//notice to List
return PartialView("_rawmat",ti);
}
I have added .ToList() to the end of the query.
I think the issue is in the second call of #Html.Partial("_rawmat") inside your Index.
It does not pass a model parameter, so it passed the default one, which is a CDB.OrderM.
Use this instead (it renders the action instead of the partial view):
#Html.Action("Reqitem")
I've been working through the Contoso University tutorial at http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/, specifically part 6 (http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application) in which course checkboxes are added to the Instructor Edit view and both the Instructor and Course database tables are updated accordingly.
Unfortunately, the tutorial does not contain how to create a new instructor record and insert to both database tables.
Can anybody offer guidance how to amend the Edit methods to do so?
UPDATE
I'm new to entity framework but am I right in thinking .Add will perform both an insert or an update?
If so, I'm wondering if all I need to change is this bit
var instructorToUpdate = db.Instructors
.Include(i => i.OfficeAssignment)
.Include(i => i.Courses)
.Where(i => i.InstructorID == id)
.Single();
to create a new id rather than use an existing id?
END OF UPDATE
Http Post Action Method
[HttpPost]
public ActionResult Edit(int id, FormCollection formCollection, string[] selectedCourses)
{
var instructorToUpdate = db.Instructors
.Include(i => i.OfficeAssignment)
.Include(i => i.Courses)
.Where(i => i.InstructorID == id)
.Single();
if (TryUpdateModel(instructorToUpdate, "", null, new string[] { "Courses" }))
{
try
{
if (String.IsNullOrWhiteSpace(instructorToUpdate.OfficeAssignment.Location))
{
instructorToUpdate.OfficeAssignment = null;
}
UpdateInstructorCourses(selectedCourses, instructorToUpdate);
db.Entry(instructorToUpdate).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
catch (DataException)
{
//Log the error (add a variable name after DataException)
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
}
PopulateAssignedCourseData(instructorToUpdate);
return View(instructorToUpdate);
}
Other Methods
private void UpdateInstructorCourses(string[] selectedCourses, Instructor instructorToUpdate)
{
if (selectedCourses == null)
{
instructorToUpdate.Courses = new List<Course>();
return;
}
var selectedCoursesHS = new HashSet<string>(selectedCourses);
var instructorCourses = new HashSet<int>
(instructorToUpdate.Courses.Select(c => c.CourseID));
foreach (var course in db.Courses)
{
if (selectedCoursesHS.Contains(course.CourseID.ToString()))
{
if (!instructorCourses.Contains(course.CourseID))
{
instructorToUpdate.Courses.Add(course);
}
}
else
{
if (instructorCourses.Contains(course.CourseID))
{
instructorToUpdate.Courses.Remove(course);
}
}
}
}
...
private void PopulateAssignedCourseData(Instructor instructor)
{
var allCourses = db.Courses;
var instructorCourses = new HashSet<int>(instructor.Courses.Select(c => c.CourseID));
var viewModel = new List<AssignedCourseData>();
foreach (var course in allCourses)
{
viewModel.Add(new AssignedCourseData
{
CourseID = course.CourseID,
Title = course.Title,
Assigned = instructorCourses.Contains(course.CourseID)
});
}
ViewBag.Courses = viewModel;
}
Edit View
#model ContosoUniversity.Models.Instructor
#{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Instructor</legend>
#Html.HiddenFor(model => model.PersonID)
<div class="editor-label">
#Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.LastName)
#Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.FirstMidName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FirstMidName)
#Html.ValidationMessageFor(model => model.FirstMidName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.HireDate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.HireDate)
#Html.ValidationMessageFor(model => model.HireDate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.OfficeAssignment.Location)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.OfficeAssignment.Location)
#Html.ValidationMessageFor(model => model.OfficeAssignment.Location)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.OfficeAssignment.Location)
</div>
<div class="editor-field">
<table style="width: 100%">
<tr>
#{
int cnt = 0;
List<ContosoUniversity.ViewModels.AssignedCourseData> courses = ViewBag.Courses;
foreach (var course in courses) {
if (cnt++ % 3 == 0) {
#: </tr> <tr>
}
#: <td>
<input type="checkbox"
name="selectedCourses"
value="#course.CourseID"
#(Html.Raw(course.Assigned ? "checked=\"checked\"" : "")) />
#course.CourseID #: #course.Title
#:</td>
}
#: </tr>
}
</table>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
Question already answered here
I guess I should have searched harder before asking the question!