Is there a way to dynamically set model values in a controller? - c#

Model
public class CustomPInfo
{
public List<PSelect> PSelectList { get; set; }
public List<ExcludeItem> ExcludeItemList { get; set; }
public List<customP> CustomPList { get; set; }
}
public class PSelect
{
public string BClass { get; set; }
public int BClassId { get; set; }
public bool Lvl1 { get; set; }
public bool Lvl2 { get; set; }
public bool Lvl3 { get; set; }
public bool Lvl4 { get; set; }
}
public class ExcludeItem
{
public string Item{ get; set;}
public Nullable<int> BClassId { get; set; }
public bool Exclude { get; set; }
}
public partial class customP
{
public int id { get; set; }
public int Lvl {get; set;}
public Nullable<bool> item1 { get; set; }
public Nullable<bool> item2 { get; set; }
public Nullable<bool> item3 { get; set; }
...
public Nullable<bool> item10 { get; set; }
}
Controller
public ActionResult CustomPanel()
{
CustomPInfo customPInfo = new CustomPInfo();
customPanelInfo.PSelectList = new List<PSelect>();
var pSelectList = from p in db.BClass select new
PSelect { BClass = p.BClass, BClassId = p.id };
foreach (var item in pSelectList)
{
customPInfo.PSelectList.Add(item);
}
customPInfo.ExcludeItemList = new List<ExcludeItem>();
var excludeItemList = from p in db.Item.OrderBy(p => p.BClass_id) select new ExcludeItem { Item = p.Name, BClassId = p.BClass_id };
foreach (var item in excludeItemList)
{
customPInfo.ExcludeItemList.Add(item);
}
customPInfo.CustomPList = new List<customP>();
customP customP = new customP();
var i = 1;
do
{
customP.lvl = i;
customPInfo.CustomPList.Add(customP);
++i;
} while (i <= 4);
return View(customPInfo);
}
[HttpPost]
public ActionResult CustomP(CustomPInfo customPInfo)
{
foreach (var X in customPInfo.ExcludeItemList)
{
if (X.Exclude == false)
{
foreach (var item in customPInfo.PSelectList)
{
if (X.BClassId == item.BClassId)
{
foreach (var CustomP in customPInfo.CustomPList)
{
if (CustomP.custom_panel_id == 1 && item.Lvl1 == true)
{
--How can I set the parameter value of CustomP from a variable?
CustomP. item.item = true;
}
if (CustomP.custom_panel_id == 2 && item.Lvl2 == true)
{
CustomP. item.item = true;
}
if (CustomP.custom_panel_id == 3 && item.Lvl3 == true)
{
CustomP. item.item = true;
}
if (CustomP.custom_panel_id == 4 && item.Lvl4 == true)
{
CustomP. item.item = true;
}
}
}
}
}
}
return View();
}
View
#{oldBClassId = 0}
#foreach (var X in Model.ExcludeItemList)
{
foreach (var item in Model.PSelectList)
{
if (X.BClassId == item.BClassId)
{
<tr>
#if (oldBClassId != item.BClassId)
{
<td style="border-top: 1px solid #cdd0d4" align="center">
#Html.CheckBoxFor(modelItem => item.Lvl1, new { id = "CBLVL1" })
</td>
<td style="border-top: 1px solid #cdd0d4" align="center">
#Html.CheckBoxFor(modelItem => item.Lvl2, new { id = "CBLVL2" })
</td>
<td style="border-top: 1px solid #cdd0d4" align="center">
#Html.CheckBoxFor(modelItem => item.Lvl3, new { id = "CBLVL3" })
</td>
<td style="border-top: 1px solid #cdd0d4" align="center">
#Html.CheckBoxFor(modelItem => item.Lvl4, new { id = "CBLVL4" })
</td>
<td style="border-top: 1px solid #cdd0d4">
#Html.DisplayFor(modelItem => item.BClass)
#Html.HiddenFor(modelItem => item.BClassId)
</td>
<td style="border-top: 1px solid #cdd0d4">
#Html.DisplayFor(modelItem => X.item)
#Html.HiddenFor(modelItem => X.ClassId)
</td>
<td style="border-top: 1px solid #cdd0d4" align="center">
#Html.CheckBoxFor(modelItem => X.Exclude)
</td>
{
oldBClassId = item.BClassId;
}
}
else
{
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
#Html.DisplayFor(modelItem => X.item)
#Html.HiddenFor(modelItem => X.BClassId)
</td>
<td align="center">
#Html.CheckBoxFor(modelItem => X.Exclude)
</td>
}
</tr>
}
}
The View is a table of checkboxes that allow someone to select a group of items and exclude a single item of that group.
In the controller is there a way to loop through a list of the item's and be able to update the model properties? This would allow the setting of individual items in 4 customPs from a selected checkbox in the group and exclude individual items in all of the 4 customPs

As str variable is a string , you should access properties of an object with string like this object[string].
foreach (var str in itemList)
{
model[str] = true;
}

Related

The model item passed into the ViewDataDictionary is of type Models but needs ViewModels

I've been struggling with this error for a few days now for an assignment and I can't get it fixed, I've shown it to another person and we are bewildered as to what could be the issue? "InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'FYP_RSVP_MGMT.Models.GuestList', but this ViewDataDictionary instance requires a model item of type 'FYP_RSVP_MGMT.ViewModels.GuestListViewModel'."
GuestListController:
public class GuestListController : Controller
{
public IActionResult Index()
{
GuestListViewModel guest = new GuestListViewModel();
return View("Index", guest);
}
public IActionResult CreateUpdate(GuestListViewModel guest)
{
if(ModelState.IsValid)
{
using (var db = DbHelpers.GetConnection()) {
if(guest.EditableGuest.GuestID == null)
{
/* Count the existing IDs and adds the next ID */
guest.EditableGuest.GuestID = guest.Guests.Count;
db.Insert<GuestList>(guest.EditableGuest);
}
/* If the guest already exists, we are updating the details*/
else
{
GuestList dbItem = db.Get<GuestList>(guest.EditableGuest.GuestID);
TryUpdateModelAsync<GuestList>(dbItem, "EditableGuest");
db.Update<GuestList>(dbItem);
}
}
return RedirectToAction("ViewGuestList");
}
else
{
return View("Index", new GuestList());
}
}
GuestListViewModel
public class GuestListViewModel
{
public GuestListViewModel()
{
using (var db = DbHelpers.GetConnection())
{
this.EditableGuest = new GuestList();
this.Guests = db.Query<GuestList>("Select * From GuestList").ToList();
}
}
public List <GuestList> Guests { get; set; }
/* Holds any instance of an object that is being added/edited/deleted etc */
public GuestList EditableGuest { get; set; }
}
GuestList Model
public class GuestList
{
public int? GuestID { get; set; }
[Required]
public string GuestName { get; set; }
[Required]
public string GuestType { get; set; }
[Required]
public string ContactDetails { get; set; }
public bool PlusOne { get; set; }
public string PlusOneName { get; set; }
[Required]
public string Response { get; set; }
}
Index.cshtml - where form is being displayed
#model FYP_RSVP_MGMT.ViewModels.GuestListViewModel
#{
ViewData["Title"] = "Guest RSVP";
}
#using (var form = Html.BeginForm("CreateUpdate", "GuestList", FormMethod.Post))
{
<div class="container" id="GuestRSVP">
<br/>
<br/>
<br/>
<h3> Welcome to [Bride] and [Groom]s Wedding</h3>
<h4>[Church Location]</h4>
<h4>[Wedding Date and Time]</h4>
<div class="container" id="RSVPTable" style="align-content:center">
<table>
<tr>
<td>Guest Name: </td>
<td>#Html.TextBoxFor(m => m.EditableGuest.GuestName)</td>
<td>#Html.HiddenFor(m => m.EditableGuest.GuestID)</td>
</tr>
<tr>
<td>Guest Type: </td>
<td>#Html.DropDownListFor(m => m.EditableGuest.GuestType, new List<SelectListItem> { new SelectListItem { Value = "Guest", Text = "Guest" }, new SelectListItem { Value = "Wedding Party", Text = "Wedding Party" } })</td>
</tr>
<tr>
<td>Contact Details: </td>
<td>#Html.TextBoxFor(m => m.EditableGuest.ContactDetails)</td>
</tr>
<tr>
<td>Plus One: </td>
<td>#Html.CheckBoxFor(m => m.EditableGuest.PlusOne)</td>
</tr>
<tr>
<td>Plus One Name: </td>
<td>#Html.TextBoxFor(m => m.EditableGuest.PlusOneName)</td>
</tr>
<tr>
<td>RSVP Response:</td>
<td>#Html.DropDownListFor(m => m.EditableGuest.Response, new List<SelectListItem> { new SelectListItem { Value = "Accept with Pleasure", Text = "Accept with Pleasure" }, new SelectListItem { Value = "Decline with Regret", Text = "Decline with Regret" } })</td>
</tr>
<tr>
<td><button class="btnSubmitRSVP" type="submit"> #(Model.EditableGuest.GuestID > 0? "Update": "Add") </button></td>
<td></td>
</tr>
</table>
</div>
</div>
}
We cannot figure out why the model is being passed and not the viewmodel? My code doesn't refer to the model? Any help would be much appreciated!
I'd say it's coming from here:
You're sending a GuestList in the else despite promising you'd send a GuestListViewModel at the top of the page:

LINQ Bind in List

i want to bind in list, how to do that, please see my code below
this below is my Controller
var transaction = await (from a in _context.Transaction group a by a.Nobukti into pg
join b in _context.ChartAccount on pg.FirstOrDefault().Kodeakun
equals b.Kodeakun
select new Transaksi
{
Nobukti = pg.FirstOrDefault().Nobukti,
Tanggal = pg.FirstOrDefault().Tanggal,
Keterangan = pg.FirstOrDefault().Keterangan,
Trans = pg.ToList()
}).ToListAsync();
return View(transaction);
See my Trans = pg.ToList() in Controller, inside pg.ToList() there are Kodeakun, and i want bind to Kodeakun = a.Kodeakun + b.Kodeakun
This below is my Model
public class ChartAccount
{
[Key]
public string Kodeakun { get; set; }
public string Namaakun { get; set; }
[DisplayFormat(DataFormatString = "{0:C2}", ApplyFormatInEditMode = true)]
public decimal Saldo { get; set; }
}
public class Transaksi
{ [Key]
public int Iud { get; set; }
public int Id { get; set; }
public string Nobukti { get; set; }
public string Kodeakun { get; set; }
public string Keterangan { get; set; }
[DataType(DataType.Date)]
public DateTime Tanggal { get; set; }
[DisplayFormat(DataFormatString = "{0:C2}", ApplyFormatInEditMode = false)]
public decimal Debit { get; set; }
[DisplayFormat(DataFormatString = "{0:C2}", ApplyFormatInEditMode = false)]
public decimal Kredit { get; set; }
public virtual List<Transaksi> Trans { get; set; }
}
this below is my view
<table class="table">
#foreach (var item in Model)
{
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Nobukti) :
#Html.DisplayFor(modelItem => item.Nobukti)
</th>
<th>
#Html.DisplayNameFor(model => model.Keterangan) :
#Html.DisplayFor(modelItem => item.Keterangan)
</th>
<th>
#Html.DisplayNameFor(model => model.Tanggal) :
#Html.DisplayFor(modelItem => item.Tanggal)
</th>
</tr>
</thead>
<tbody>
#foreach (var transaksi in item.Trans)
{
<tr>
<td>
#Html.DisplayFor(modelItem => transaksi.Kodeakun)
</td>
<td>
#if (transaksi.Debit == 0) { }
else
{
#Html.DisplayFor(modelItem => transaksi.Debit)
}
</td>
<td>
#if (transaksi.Kredit == 0) { }
else
{
#Html.DisplayFor(modelItem => transaksi.Kredit)
}
</td>
</tr>
}
</tbody>
}
</table>
sorry for my bad english, i use ASP Net Core 2.2

C# , MVC , Instead table html

I have this model :
public class CoursesTeacher
{
public int Id { get; set; }
public string FullName { get; set; }
public IEnumerable<string> CourseName { get; set; }
public IEnumerable<int> CourseId { get; set; }
}
I would like to create an Instead table html by razor.
The teacher has a multiple courses.
like that
that
I wrote this code :
<table class="table table-bordered">
<tr>
<th>
#Html.ActionLink("Teacher Name", "RequestsTeachers")
</th>
<th>
#Html.ActionLink("Corese Name", "RequestsTeachers")
</th>
<th>
Edit
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td rowspan="#item.CourseName.Count()">
#Html.DisplayFor(modelItem => item.FullName)
</td>
#foreach (var courseName in item.CourseName)
{
<td>
#Html.DisplayFor(modelItem => courseName)
</td>
}
#foreach (var courseId in item.CourseId)
{
<td>
<div class="pull-right">
#Html.NoEncodeActionLink("<span class='glyphicon glyphicon-pencil'></span>", "Edit", "EditFinancial", "Control", routeValues: new { courseId = courseId }, htmlAttributes: new { data_modal = "", #class = "btn btn-default" })
</div>
</td>
}
</tr>
}
but created not correct :
correct
how to create correct ?
I'd go and create a viewmodel to represent the info for a course:
public class CourseInfoModel
{
public string CourseName { get; set; }
public int CourseId { get; set; }
public bool IsFirstCourse { get; set; }
}
Then in the model class, create a readonly property to get the data in a more suitable way:
public class CoursesTeacher
{
public int Id { get; set; }
public string FullName { get; set; }
public IEnumerable<string> CourseName { get; set; }
public IEnumerable<int> CourseId { get; set; }
public IList<CourseInfoModel> Courses
{
get
{
if (CourseName == null || CourseId == null || CourseId.Count() != CourseName.Count())
throw new Exception("Invalid data"); //courses and ids must have the same number of elements
var list = new List<CourseInfoModel>();
for (int i = 0; i < CourseName.Count(); i++)
{
list.Add(new CourseInfoModel
{
IsFirstCourse = i == 0,
CourseId = CourseId.ElementAt(i),
CourseName = CourseName.ElementAt(i),
});
}
return list;
}
}
}
Now in the view is easy to render the table the way you want:
#model IEnumerable<MvcTable.Models.CoursesTeacher>
<table class="table table-bordered">
<tr>
<th>
#Html.ActionLink("Teacher Name", "RequestsTeachers")
</th>
<th>
#Html.ActionLink("Course Name", "RequestsTeachers")
</th>
<th>
Edit
</th>
</tr>
#foreach (var item in Model)
{
foreach (var courseItem in item.Courses)
{
<tr>
#if (courseItem.IsFirstCourse)
{
<td rowspan="#item.Courses.Count">
#Html.DisplayFor(modelItem => item.FullName)
</td>
}
<td>
#Html.DisplayFor(modelItem => courseItem.CourseName)
</td>
<td>
<div class="pull-right">
<span class='glyphicon glyphicon-pencil'></span>
</div>
</td>
</tr>
}
}
</table>
Instead of keeping the CourseName and CourseIds to seperate lists. Why not create a class to represent a course and use that ?
public class CourseVm
{
public int Id { set; get;}
public string Name { set; get;}
}
public class CoursesTeacher
{
public int Id { set;get;}
public string FullName { set;get;}
public List<CourseVm> Courses { set;get;}
}
and in the view which is strongly typed to a collection CoursesTeacher
#model IEnumerable<CoursesTeacher>`
<table>
#foreach(var t in Model)
{
<tr>
<td>#t.FullName</td>
<td>
<table>
#foreach(var c in t.Courses)
{
<tr><td>#c.Name</td>
<td><button>Some button</button></td>
</tr>
}
</table>
</td>
</tr>
}
</table>

Related data not showing asp.net c#

After reading this tutorial http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application I have created some models, controllers and views.
The recipes are showing just fine in the view, but I can't get the RecipeLines to show.
RecipeModel
public class RecipeModel
{
[Key]
public int RecipeId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }
public virtual ICollection<RecipeLine> RecipeLines { get; set; }
}
RecipeLine
public class RecipeLine
{
[Key]
public int RecipeLineId { get; set; }
public int RecipeId { get; set; }
public double Quantity { get; set; }
public UnitOfMeasureModel UnitOfMeasure { get; set; }
public IngredientModel Ingredient { get; set; }
}
RecipeViewModel
public class RecipeViewModel
{
public IEnumerable<RecipeModel> RecipeModels { get; set; }
public IEnumerable<RecipeLine> RecipeLines { get; set; }
}
Recipecontroller
public class RecipeController : Controller
{
private RecipeApplicationDb db = new RecipeApplicationDb();
[HttpGet]
public ActionResult Index(int? id)
{
var viewModel = new RecipeViewModel();
viewModel.RecipeModels = db.Recipes
//.Include(i => i.Name)
.Include(i => i.RecipeLines);
if (id != null)
{
ViewBag.RecipeId = id.Value;
viewModel.RecipeLines = viewModel.RecipeModels.Where(i => i.RecipeId == id.Value).Single().RecipeLines;
}
return View(viewModel);
}
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
RecipeModel recipeModel = db.Recipes.Find(id);
if (recipeModel == null)
{
return HttpNotFound();
}
return View(recipeModel);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
And the view
#model RecipeApplication.Models.RecipeViewModel
#{
ViewBag.Title = "Recepten";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
Naam
</th>
<th>
Omschrijving
</th>
<th>
Afbeelding
</th>
</tr>
#foreach (var item in Model.RecipeModels) {
string selectedRow = "";
if(item.RecipeId == ViewBag.RecipeId)
{
selectedRow = "success";
}
<tr class="#selectedRow" valign="top">
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#if (item.ImageUrl != null)
{
#Html.DisplayFor(modelItem => item.ImageUrl)
}
</td>
<td>
#Html.ActionLink("Select", "Index", new { id = item.RecipeId}) |
#Html.ActionLink("Edit", "Edit", new { id=item.RecipeId }) |
#Html.ActionLink("Details", "Details", new { id=item.RecipeId }) |
#Html.ActionLink("Delete", "Delete", new { id=item.RecipeId })
</td>
</tr>
}
</table>
#if (Model.RecipeLines != null)
{
foreach (var item in Model.RecipeLines)
{
string selectedRow = "";
if (item.RecipeId == ViewBag.id)
{
<p>#item.Quantity #item.UnitOfMeasure #item.Ingredient</p>
}
}
}
When selecting the recipe, the line does get a proper color, and I see an id-value in the URL-string.
If someone could help with this one, that would be awesome.
You're comparing item.RecipeId to ViewBag.id, which doesn't exist. The ViewBag member you set in the controller action was ViewBag.RecipeId.
However, you don't need this conditional at all. All of the recipe lines are already for that recipe id, because you specifically set only those recipe items in Model.RecipeLines.
//change your controller action
[HttpGet]
public ActionResult Index(int? id)
{
if(id == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var model = new RecipeViewModel();
var data = db.RecipeModel.Include(i => i.RecipeLines)
.Where(x=>x.RecipeId == id)
.ToList();
model.RecipeModels = data;
return View(model);
}
//change your viewModel
public class RecipeViewModel
{
public IEnumerable<RecipeModel> RecipeModels { get; set; }
}
//this is in the view
#if (Model.RecipeLines != null)
{
foreach (var item in Model.RecipeModels.RecipeLines)
{
<p>
#item.Quantity
#item.UnitOfMeasure
#item.Ingredient
</p>
}
}

Paging In MVC Table Grid

What I am trying to achieve is to include paging functionality in a grid (generated with an html table and a foreach statement).
I am not allowed to use entity framework, webgrid, or any other third party grid.
Below are the details of my task:
DemoTestData.cs file
namespace Demo.Data
{
public class DemoTestData
{
DemoTestModel objdemoTestModel = null;
public string connectionString { get; set; }
public SqlConnection objConnection { get; set; }
public SqlCommand objCommand { get; set; }
public SqlDataReader objReader { get; set; }
public List<DemoTestModel> GetAllDemoTest()
{
List<DemoTestModel> listDemoTest = new List<DemoTestModel>();
try
{
objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
objConnection.Open();
objCommand = new SqlCommand("sp_DemoTest", objConnection);
objCommand.CommandType = System.Data.CommandType.StoredProcedure;
objReader = objCommand.ExecuteReader();
if (objReader.HasRows)
{
while (objReader.Read())
{
// list will be populated here.
objdemoTestModel = new DemoTestModel();
objdemoTestModel.Id = Convert.ToInt32(objReader["Id"].ToString());
objdemoTestModel.Column1 = objReader["Column1"].ToString();
objdemoTestModel.Column2 = objReader["Column2"].ToString();
objdemoTestModel.Column3 = objReader["Column3"].ToString();
objdemoTestModel.Column4 = objReader["Column4"].ToString();
objdemoTestModel.Column5 = objReader["Column5"].ToString();
objdemoTestModel.Column6 = objReader["Column6"].ToString();
listDemoTest.Add(objdemoTestModel);
}
//return listStateModel;
}
}
catch (Exception ex)
{
string m = ex.Message;
}
finally
{
if (objConnection != null)
{
objConnection.Close();
}
}
return listDemoTest;
}
}
}
sp_DemoTest procedure
CREATE PROCEDURE [dbo].[sp_DemoTest]
AS
BEGIN
select Id,Column1,Column2,Column3,Column4,Column5,Column6 from dbo.DemoTest
END
DemoTestModel.cs file
namespace Demo.Entities
{
public class DemoTestModel
{
public int Id { get; set; }
public string Column1 { get; set; }
public string Column2 { get; set; }
public string Column3 { get; set; }
public string Column4 { get; set; }
public string Column5 { get; set; }
public string Column6 { get; set; }
}
}
DemoTestController.cs file
namespace SampleProject.Controllers
{
public class DemoTestController : Controller
{
// GET: /DemoTest/
//Test tst = new Test();
DemoTestData data = new DemoTestData();
//List<CountryModel> ListModel = new List<CountryModel>();
List<DemoTestModel> ListModel = new List<DemoTestModel>();
//DemoTestModel demoTestModel = new DemoTestModel();
public ActionResult Index()
{
ListModel = data.GetAllDemoTest();
return View(ListModel);
}
}
}
Index.cshtml file
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Column1)
</th>
<th>
#Html.DisplayNameFor(model => model.Column2)
</th>
<th>
#Html.DisplayNameFor(model => model.Column3)
</th>
<th>
#Html.DisplayNameFor(model => model.Column4)
</th>
<th>
#Html.DisplayNameFor(model => model.Column5)
</th>
<th>
#Html.DisplayNameFor(model => model.Column6)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#if (item.Column1 == "Textbox")
{
#Html.TextBoxFor(modelItem => item.Column1)
}
else if (item.Column1 == "Dropdown")
{
#Html.DropDownListFor(modelItem => item.Column1, Enumerable.Empty<SelectListItem>())
}
else
{
#Html.DisplayFor(modelItem => item.Column1)
}
</td>
<td>
#if (item.Column2 == "Textbox")
{
#Html.TextBoxFor(modelItem => item.Column2)
}
else if (item.Column2 == "Dropdown")
{
#Html.DropDownListFor(modelItem => item.Column2, Enumerable.Empty<SelectListItem>())
}
else
{
#Html.DisplayFor(modelItem => item.Column2)
}
<td>
#if (item.Column3 == "Textbox")
{
#Html.TextBoxFor(modelItem => item.Column3)
}
else if (item.Column3 == "Dropdown")
{
#Html.DropDownListFor(modelItem => item.Column3, Enumerable.Empty<SelectListItem>())
}
else
{
#Html.DisplayFor(modelItem => item.Column3)
}
</td>
<td>
#if (item.Column4 == "Textbox")
{
#Html.TextBox("test", "", new { style = "width:130px;" })
}
else if (item.Column4 == "Dropdown")
{
#Html.DropDownListFor(modelItem => item.Column4, Enumerable.Empty<SelectListItem>())
}
else
{
#Html.DisplayFor(modelItem => item.Column4)
}
</td>
<td>
#if (item.Column5 == "Textbox")
{
#Html.TextBox("test1", "", new { style = "width:130px;" })
}
else if (item.Column5 == "Dropdown")
{
#Html.DropDownListFor(modelItem => item.Column5, Enumerable.Empty<SelectListItem>(), new { style = "width: 130px" })
}
else
{
#Html.DisplayFor(modelItem => item.Column5)
}
</td>
<td>
#if (item.Column6 == "Textbox")
{
#Html.TextBoxFor(modelItem => item.Column6)
}
else if (item.Column6 == "Dropdown")
{
#Html.DropDownListFor(modelItem => item.Column6, Enumerable.Empty<SelectListItem>())
}
else
{
#Html.DisplayFor(modelItem => item.Column6)
}
</td>
</tr>
}
</table>
(if..else condition is used to generate dynamic controls)
This is the rendered view
It would be very nice if anyone could provide a solution.

Categories

Resources