I have a MultiSelect belongs to Kendo ListView EditorTemplates. When I want to submit selected MultiSelect values, I got list of items selected, but all populated with 0 value. I cant get correct value of selected items.
Here is my ListView:
#(Html.Kendo().ListView<esCMS.Modules.C2C.Domain.c2cFieldRangeList>()
.Name("listView")
.TagName("div")
.ClientTemplateId("template")
.DataSource(dataSource => dataSource
.Model(model => model.Id(x => x.FieldRangeID))
.PageSize(6)
.Create(create => create.Action("AddRange", "Field"))
.Update(update => update.Action("UpdateRange", "Field"))
.Read(read => read.Action("ReadRange", "Field", new { id = Model.FieldID }))
))
And here is my EditorTemplates:
<table>
<tr>
<td>
#Html.LabelFor(x => x.RangeValue)
</td>
<td>
:
</td>
<td>
#Html.TextBoxFor(x => x.RangeValue, new { #class = "k-textbox", style = "width:180px" })
<span data-for="RangeValue" class="k-invalid-msg"></span>
</td>
</tr>
<tr>
<td>
#Html.LabelFor(x => x.SelectedValues)
</td>
<td>
:
</td>
<td>
#(Html.Kendo().MultiSelectFor(x=> x.SelectedValues)
.HtmlAttributes(new { style = "width:180px" })
.BindTo(new SelectList(ViewBag.Fields, "FieldID", "Title")))
</td>
</tr></table>
Edit: ViewBag.Fields contains List of c2cFieldList model and I expect to get List of FieldID when I submit view.
ViewBag.Fields = Entities.c2cField.Where(x => !x.IsGeneral && !x.IsTemp).Select(x => new c2cFieldList { Title = x.Title, FieldID = x.FieldID});
Any advice will be helpful.
Related
my problem it's kinda strange. So in a Index.cshtml i populate with data and in some place i use
#Html.Action("_Create")
To create a partialView where i populate with some data from the Server and with some empty fields that need to be completed for submit.
[HttpGet]
public PartialViewResult _Create()
{
var userOrder = _orderRepository.CurrentUser(User.Identity.GetUserName());
var something = new CurrentListOrderViewModel()
{
PersonName = userOrder.Select(p=>p.PersonName).FirstOrDefault(),
Funds = userOrder.Select(p=>p.Funds).FirstOrDefault(),
Order = "",
OrderCosts = 0,
Restaurant = "",
TodayOrderDate = DateTime.Now,
WindowsName = User.Identity.GetUserName()
};
return PartialView(something);
}
[HttpPost]
[ValidateAntiForgeryToken]
public PartialViewResult _Create([Bind(Include = "PersonName,Funds,Order,OrderCosts,WindowsName,TodayOrderDate")]CurrentListOrderViewModel model)
{
if(ModelState.IsValid)
{
return PartialView();
}
return PartialView(model);
}
So the problem is when the page loads with HTTPGET everything goes fine, the table get's populated from the database and if i change in the _Create.cshtml the #Html.EditorFor into something else so i can disable it, i lose the data. What could be the cause? I don't really understand it.
#model FoodOrderQubizInterfaces.ViewModel.Order.CurrentListOrderViewModel
#using(Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<tr>
<td>
#Html.EditorFor(model => model.PersonName)
#Html.ValidationMessageFor(model => model.PersonName)
</td>
<td>
#Html.EditorFor(model => model.Funds, new { htmlAttributes = new { disabled = "disabled" }, })
#Html.ValidationMessageFor(model => model.Funds)
</td>
<td>
#Html.EditorFor(model => model.Order)
#Html.ValidationMessageFor(model => model.Order)
</td>
<td>
#*#Html.DropDownListFor(model => model.Restaurant,*#
<select>
<option value="1">Bigys</option>
<option value="2">Zulu</option>
</select>
</td>
<td>
#Html.EditorFor(model => model.OrderCosts)
#Html.ValidationMessageFor(model => model.OrderCosts)
</td>
<td>
#Html.EditorFor(model => model.WindowsName)
#Html.ValidationMessageFor(model => model.WindowsName)
<input type="submit" value="Save/Modify" class="btn btn-default" />
</td>
<td>
#Html.EditorFor(model => model.TodayOrderDate)
#Html.ValidationMessageFor(model => model.TodayOrderDate)
</td>
<td>
</td>
</tr>
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
Hope you guys can help me with some advice cause i'm out of ideas
I found the problem, after i search the web for some time. The cause was when you submit and you have the disabled attribute added it will not add to the model cause he thinks the model is disabled so the user couldn't interact with it.
So to fix it i changed from
<td>
#Html.EditorFor(model => model.Funds, new { htmlAttributes = new { disabled = "disabled" }, })
#Html.ValidationMessageFor(model => model.Funds)
</td>
To
#Html.TextBoxFor(model => model.Funds, new { #readonly = true })
And in that was it keeps the data. I hope it will help someone else with my problem
I am developing a MVC 3 Razor app and I'm having issues when I click the back button in the browser. My application workflow:
Select a facility from a dropdown list
A WebGrid gets populated with a list of the facility's buildings.
Click an image to edit a building
Click the browser's back button and the dropdown list in step 1 appears without the CSS formatting. If I click F5 to refresh then everything resets and the CSS formatting is back.
I'm using VS2010 and the ASP.NET development server with IE9 as the default browser. I have added the OutputCache attribute to every ActionResult in the controller.
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
Here's my link in the WebGrid built from a PartialView
grid.Column(header: "",
format: #<text><a href="#Url.Action("Edit", "BuildingModels", new { #id = item.FACInventoriesId })"><img src="#Url.Content("~/Content/images/edit.png")"
alt='Edit' title='Edit' border='0'/></a></text>)
How do I get a browser to show the WebGrid (step 2) when the back button is click from editing a building (step 4)? Also any ideas why the CSS formatting is missing when I click the back button?
Here's the Controller code:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public ViewResult Index()
{
ViewBag.Systems = buildingsVM.GetSystemsList();
return View();
}
[HttpPost]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public ActionResult GetFacilityDetails(int systemId, string facilityId)
{
try
{
ViewBag.Systems = buildingsVM.GetSystemsList();
var facility = buildingsVM.GetFacilityDetails(systemId, facilityId);
facility.Buildings = buildingsVM.GetFacilityBuildings(systemId, facilityId);
var bldgsHtml = ViewsUtility.RenderPartialViewToString(this, "_Buildings", facility.Buildings);
TempData["CurrentFacility"] = facility;
return Json(new { ok = true, facdata = facility, bldgs = bldgsHtml, message = "ok" });
}
catch (Exception ex)
{
return Json(new { ok = false, message = ex.Message });
}
}
[HttpPost]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public ActionResult GetSystemFacilities(int systemId)
{
try
{
var facilities = buildingsVM.GetFacilitesBySystemId(systemId);
return Json(new { ok = true, data = facilities, message = "ok" });
}
catch (Exception ex)
{
return Json(new { ok = false, message = ex.Message });
}
}
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public ActionResult Edit(int id)
{
var facility = TempData["CurrentFacility"] as FacilityModel;
return View(buildingsVM.GetBuilding(id));
}
Code from Partial View:
#model IEnumerable<COPSPlanningWeb.Models.BuildingModel>
<!-- Current Buildings from partial view -->
#{
if (Model != null && Model.Count() > 0)
{
var grid = new WebGrid(rowsPerPage: 50, defaultSort: "BuildingNumber"); //ajaxUpdateContainerId: "tabs-2",
grid.Bind(Model, rowCount: Model.Count(), autoSortAndPage: false);
grid.Pager(WebGridPagerModes.All);
#grid.GetHtml(
tableStyle: "webgridDisplay",
alternatingRowStyle: "alt",
columns: grid.Columns(
//grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { EmployeeID = item.EmployeeID, ContactID = item.ContactID })),
grid.Column("BuildingNumber", header: "Building Number", style: "webgridDisplayCenter"),
grid.Column("ConstructionDate", header: "Construction Date", format: #<text>#item.ConstructionDate.ToString("MM/dd/yyyy")</text>),
grid.Column("ExtSquareFeet", header: "Exterior Sq. Ft.", format: (item) => string.Format("{0:n0}", item.ExtSquareFeet)),
grid.Column("IntSquareFeet", header: "Interior Sq. Ft.", format: (item) => string.Format("{0:n0}", item.IntSquareFeet)),
grid.Column("IU_Avail", header: "IU Available"),
grid.Column("SpaceAvail", header: "Space Available"),
grid.Column("FixedAssetValue", header: "Fixed Asset Value", format: (item) => string.Format("{0:C}", item.FixedAssetValue)),
grid.Column("FixedEquipValue", header: "Fixed Equipment Value", format: (item) => string.Format("{0:C}", item.FixedEquipValue)),
grid.Column(header: "",
format: #<text><a href="#Url.Action("Edit", "BuildingModels", new { #id = item.FACInventoriesId })"><img src="#Url.Content("~/Content/images/edit.png")"
alt='Edit' title='Edit' border='0'/></a></text>),
grid.Column(header: "",
format: #<text><a href="#Url.Action("Delete", "BuildingModels", new { #id = item.FACInventoriesId })"><img src="#Url.Content("~/Content/images/trash.png")"
alt='Delete' title='Delete' border='0'/></a></text>)
))
}
}
Code from Edit view:
#model COPSPlanningWeb.Models.BuildingModel
#{
ViewBag.Title = "Add/Edit Inventory";
}
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<table style="width: 100%;" class="display">
#Html.HiddenFor(model => model.FacilityId)
#Html.HiddenFor(model => model.FACInventoriesId)
<tr>
<th colspan="2" style="text-align: left;">
Building Information - Edit Inventory
</th>
</tr>
<tr>
<td class="fieldlabel">
Facility Name
</td>
<td class="fielddata">
</td>
</tr>
<tr>
<td class="fieldlabel">
Building Number
</td>
<td class="fielddata">
#Html.EditorFor(model => model.BuildingNumber)
#Html.ValidationMessageFor(model => model.BuildingNumber)
</td>
</tr>
<tr>
<td class="fieldlabel">
Construction Date
</td>
<td class="fielddata">
#Html.EditorFor(model => model.ConstructionDate, "DateTime")
#Html.ValidationMessageFor(model => model.ConstructionDate)
</td>
</tr>
<tr>
<td class="fieldlabel">
Exterior Sq. Ft.
</td>
<td class="fielddata">
#Html.EditorFor(model => model.ExtSquareFeet)
#Html.ValidationMessageFor(model => model.ExtSquareFeet)
</td>
</tr>
<tr>
<td class="fieldlabel">
Interior Sq. Ft.
</td>
<td class="fielddata">
#Html.EditorFor(model => model.IntSquareFeet)
#Html.ValidationMessageFor(model => model.IntSquareFeet)
</td>
</tr>
<tr>
<td class="fieldlabel">
IU Available
</td>
<td class="fielddata">
#Html.EditorFor(model => model.IU_Avail)
#Html.ValidationMessageFor(model => model.IU_Avail)
</td>
</tr>
<tr>
<td class="fieldlabel">
Space Available
</td>
<td class="fielddata">
#Html.EditorFor(model => model.SpaceAvail)
#Html.ValidationMessageFor(model => model.SpaceAvail)
</td>
</tr>
<tr>
<td class="fieldlabel">
Fixed Asset Value
</td>
<td class="fielddata">
#Html.EditorFor(model => model.FixedAssetValue)
#Html.ValidationMessageFor(model => model.FixedAssetValue)
</td>
</tr>
<tr>
<td class="fieldlabel">
Fixed Equipment Value
</td>
<td class="fielddata">
#Html.EditorFor(model => model.FixedEquipValue)
#Html.ValidationMessageFor(model => model.FixedEquipValue)
</td>
</tr>
<tr>
<td class="fieldlabel">
Total Fixed Asset Value
</td>
<td class="fielddata">
#Html.EditorFor(model => model.TotalFixedAssetValue)
#Html.ValidationMessageFor(model => model.TotalFixedAssetValue)
</td>
</tr>
<tr>
<td class="fieldlabel">
Total Fixed Equipment Value
</td>
<td class="fielddata">
#Html.EditorFor(model => model.TotalFixedEquipValue)
#Html.ValidationMessageFor(model => model.TotalFixedEquipValue)
</td>
</tr>
<tr>
<td colspan="2">
<table class="display" style="text-align: center;">
<tr>
<td>
#Html.ActionLink("Add/Edit Spaces", "Index")
</td>
<td>
<input type="submit" value="Save Changes" class="button" />
</td>
<td>
#Html.ActionLink("Back to Buildings List", "Index")
</td>
</tr>
</table>
</td>
</tr>
</table>
}
When I click the back button from the Edit view I expected to see the WebGrid again (building list), but instead I get the first view without any CSS formatting.
Thanks to Luis I was able to resolve the CSS formatting issue, but I'm still not seeing the WebGrid when I click the back button. I'm using JSON to populate the WebGrid could this be the problem? Should I use a form post after the item in the dropdown has been selected?
Something similar happened to me making an app for the intranet... but hey cheer up, at least your company uses IE9... I had to make miracles trying to make a MVC3 Razor app with JQuery work with IE7...
Ok now to what's important, I had a similar issue with the cache of IE, it appears that the cache of this browser works "differently" from the normal-new-age browsers, you could try this:
Press F12 and go to the tab Cache and check Always refresh from server
Then check if everything works as it should do, if it does, tell your network administrator to make a new policy for all the IE browsers that are going to use this new app you're making.
Also check this https://superuser.com/questions/81182/how-to-force-internet-explorer-ie-to-really-reload-the-page
Hope it helps!
You need to add the Location to the attribute to get this to work with IE9
[OutputCache(Location = OutputCacheLocation.ServerAndClient, NoStore = true, Duration = 0, VaryByParam = "None")]
I am using MVC 3 and Razor, attempting to post a form back to a controller from a telerik window (telerik.window.create) that loads a partial view. Im not sure how to post this so ill just post the code in order of execution and explain it as I go.
First an anchor is clicked, and onClick calls:
function showScheduleWindow(action, configId) {
var onCloseAjaxWindow = function () { var grid = $("#SubscriptionGrid").data("tGrid"); if (grid) { grid.rebind(); } };
CreateAjaxWindow("Create Schedule", true, false, 420, 305, "/FiscalSchedule/" + action + "/" + configId, onCloseAjaxWindow);
}
And the CrateAjaxWindow method:
function CreateAjaxWindow(title, modal, rezible, width, height, url, fOnClose) {
var lookupWindow = $.telerik.window.create({
title: title,
modal: modal,
rezible: rezible,
width: width,
height: height,
onClose: function (e) {
e.preventDefault();
lookupWindow.data('tWindow').destroy();
fOnClose();
}
});
lookupWindow.data('tWindow').ajaxRequest(url);
lookupWindow.data('tWindow').center().open();
}
Here is the partial view that is being loaded:
#model WTC.StatementAutomation.Web.Models.FiscalScheduleViewModel
#using WTC.StatementAutomation.Model
#using WTC.StatementAutomation.Web.Extensions
#using (Html.BeginForm("Create", "FiscalSchedule", FormMethod.Post, new { id = "FiscalScheduleConfigForm" }))
{
<div id="FiscalScheduleConfigForm" class="stylized">
<div class="top">
<div class="padding">
Using fiscal year end: #Model.FiscalYearEnd.ToString("MM/dd")
</div>
<div class="padding Period">
<table border="0">
<tr>
<th style="width: 120px;"></th>
<th>Effective Date</th>
<th>Next Run</th>
<th>Start From Previous</th>
</tr>
<tr>
<td>
#Html.CheckBoxFor(m => m.HasMonthly)
<label>Monthly</label>
</td>
<td>
#{ var month = Model.GetForFiscalPeriod(FiscalPeriodStatementSchedule.FiscalPeriod.Monthly);}
#month.BaseSchedule.StartDate.ToString("MM/01/yyyy")
</td>
<td>
#month.BaseSchedule.NextScheduleRun.ToString("MM/dd/yyyy")
</td>
<td class="previous">
#(month.HasRun ? Html.CheckBoxFor(m => month.BaseSchedule.StartFromPreviousCycle, new { #disabled = "disabled", #readonly = "readonly" }) : Html.CheckBoxFor(m => month.BaseSchedule.StartFromPreviousCycle))
</td>
</tr>
<tr>
<td>
#Html.CheckBoxFor(m => m.HasQuarterly) Quarterly
</td>
<td>
#{ var quarter = Model.GetForFiscalPeriod(FiscalPeriodStatementSchedule.FiscalPeriod.Quarterly);}
#quarter.BaseSchedule.StartDate.ToString("MM/01/yyyy")
</td>
<td>
#quarter.BaseSchedule.NextScheduleRun.ToString("MM/dd/yyyy")
</td>
<td class="previous">
#(quarter.HasRun ? Html.CheckBoxFor(m => quarter.BaseSchedule.StartFromPreviousCycle, new { #disabled = "disabled", #readonly = "readonly" }) : Html.CheckBoxFor(m => quarter.BaseSchedule.StartFromPreviousCycle))
</td >
</tr>
<tr>
<td>
#Html.CheckBoxFor(m => m.HasAnnual) Annual
</td>
<td>
#{ var annual = Model.GetForFiscalPeriod(FiscalPeriodStatementSchedule.FiscalPeriod.Annual);}
#annual.BaseSchedule.StartDate.ToString("MM/01/yyyy")
</td>
<td>
#annual.BaseSchedule.NextScheduleRun.ToString("MM/dd/yyyy")
</td>
<td class="previous">
#(annual.HasRun ? Html.CheckBoxFor(m => annual.BaseSchedule.StartFromPreviousCycle, new { #disabled = "disabled", #readonly = "readonly" }) : Html.CheckBoxFor(m => annual.BaseSchedule.StartFromPreviousCycle))
</td>
</tr>
<tr>
<td>
#Html.CheckBoxFor(m => m.HasSemiAnnual) Semi-annual
</td>
<td>
#{ var semi = Model.GetForFiscalPeriod(FiscalPeriodStatementSchedule.FiscalPeriod.SemiAnnual);}
#semi.BaseSchedule.StartDate.ToString("MM/01/yyyy")
</td>
<td>
#semi.BaseSchedule.NextScheduleRun.ToString("MM/dd/yyyy")
</td>
<td class="previous">
#(semi.HasRun ? Html.CheckBoxFor(m => semi.BaseSchedule.StartFromPreviousCycle, new { #disabled = "disabled", #readonly = "readonly" }) : Html.CheckBoxFor(m => semi.BaseSchedule.StartFromPreviousCycle))
</td>
</tr>
</table>
</div>
<div class="padding StartDay">
<span>Run on day:</span>
#Html.TextBoxFor(model => model.StartDay)
<span>of every period.</span>
</div>
</div>
<div class="bottom">
<div class="padding">
<div style="float: left;">
#if (Model.ShowSuccessSave)
{
<div id="successSave" class="label">Changes saved succesfully</div>
}
#Html.ValidationSummary(true)
#Html.HiddenFor(x => x.SubscriptionId)
#Html.HiddenFor(x => x.DeliveryConfigurationId)
#Html.HiddenFor(x => x.FiscalYearEnd)
</div>
<a id="saveSchedule" class="btn" href="">Save</a>
</div>
</div>
</div>
}
<script type="text/javascript">
$(function () {
$('a#saveSchedule').click(function () {
$(this).closest("form").submit();
return false;
});
});
</script>
And finally the controller method:
[HttpPost]
public ActionResult Create(FormCollection formValues, int subscriptionId, int deliveryConfigurationId, int startDay, DateTime fiscalYearEnd)
{
if (ModelState.IsValid)
{
var selectedSchedules = GetCheckedSchedulesFromForm(formValues);
var startFromPrevious = GetFromPreviouSelections(formValues);
this.AddModelErrors(_fiscalScheduleService.AddUpdateSchedules(selectedSchedules, subscriptionId, deliveryConfigurationId, startDay, startFromPrevious));
}
return new RenderJsonResult { Result = new { success = true, action = ModelState.IsValid ? "success" : "showErrors",
message = this.RenderPartialViewToString("_FiscalScheduleConfigForm",
BuildResultViewModel(deliveryConfigurationId, subscriptionId, fiscalYearEnd, ModelState.IsValid)) } };
}
As you can see I am using jQuery to post back to the controller, which I have done on several occasions in the applicaiton, this seems to work fine normally. But with this form, for some reason it is not posting back or stepping into the Create method at all. I am speculating that it has something to do with the parameters on the controller method. But I am fairly new to MVC (coming from ASP.NET world) so Im not really sure what I am doing wrong here. Any help would be greately appreciated!
I was able to get it to post to the controller by modifying the textboxfor for the startDay:
Changed from:
#Html.TextBoxFor(model => model.StartDay)
To:
#Html.TextBoxFor(model => model.StartDay, new { id = "startDay" })
My guess is that you're running on a virtual directory in IIS? That url you're generating is likely the culprit.
Hit F12, check out the network tab (and enable tracing) and see what it's trying to request.
Instead of building the link through text, why not use #Url.Action()? You could store this in an attribute on the a tag (like in an attribute called data-url, for example) and then use that info to make your call. It's pretty easy to pull out the attribute with jQuery, something like this:
$('.your-link-class').click(function(){
var url = $(this).attr('data-url');
// proceed with awesomesauce
});
Would something like that work for you?
[shameless self plug] As far as the controller action signature goes, you might want to look into model binding if you can. One simple class and many of your headaches will go away. You can read more here, read the parts on model binding. There are downloadable samples for different approaches.
Cheers.
I want to add an id to the "tr" elements of the mvccontrib grid I build:
<tr id="0"/>
<tr id="1"/>
so if the table contains 10 rows, the ids are 0 through to 9.
One way is to add an additional item to my entity to store this value and then create this as a hidden column with the id as the value of this item - not very elegant.
Is there a more elegant way to do this?
Thanks
I've got this far but now it complains at the RenderUsing line, any ideas?
#model IEnumerable<Tens.Models.UserPreviousNamesView>
<div class="demo_jui">
#{
var userId = 0;
foreach (var item in Model)
{
userId = item.Id;
break;
}
#(Html.Grid(Model.Select((item,index) => new { Item = item, Index = index}))
.Columns(col =>
{
col.For(p => p.Item.Title);
col.For(p => p.Item.Name);
col.Custom(#<text>
#Ajax.ActionLink("Delete", "DeleteUserPreviousName", "Summary", null, null, new { id = item.Item.Id, #class = "deleteUserPreviousName" })
</text>).Encode(false);
})
.RowAttributes(p => new Hash(Id => "id"+p.Item.Index.ToString()))
.Attributes(Id => "userPreviousNamesTable")
.Empty("You currently have no Previous Names.")
.RenderUsing(new Tens.GridRenderers.UserPreviousNamesGridRenderer<Tens.Models.UserPreviousNamesView>()));
}
You could transform the model to add it a row index and then use the RowAttributes method:
#model IEnumerable<MyViewModel>
#(Html
.Grid(Model.Select((item, index) => new { Item = item, Index = index }))
.Columns(column =>
{
column.For(x => x.Item.Foo);
column.For(x => x.Item.Bar);
})
.RowAttributes(x => new Hash(id => string.Format("id{0}", x.Item.Index)))
)
Also I have pre-pended the id with the id keyword as ids in HTML cannot statr with a number as shown in your example.
Sample output:
<table class="grid">
<thead>
<tr>
<th>Foo</th>
<th>Bar</th>
</tr>
</thead>
<tbody>
<tr id="id0" class="gridrow">
<td>foo 1</td>
<td>bar 1</td>
</tr>
<tr id="id1" class="gridrow_alternate">
<td>foo 2</td>
<td>bar 2</td>
</tr>
<tr id="id2" class="gridrow">
<td>foo 3</td>
<td>bar 3</td>
</tr>
</tbody>
</table>
You can always show hide columns without adding id to particular row or columns like below
$(".mvcGridDollarHeader th:nth-child(16)").hide();
$(".mvcGrid td:nth-child(16)").hide();
Where mvcGrid is tableStyle and mvcGridDollarHeader is header style.
#grid1.GetHtml(
tableStyle: "mvcGrid",
displayHeader: true,
emptyRowCellValue: "",
headerStyle: "mvcGridDollarHeader",
Just a quick question from a newbie to the pro's.
I am trying to implement a editable grid type form in my application. Here is the example.
#for (int i = 0; i < Model.Items.Count; i++)
{
<tr>
<td>#Html.CheckBoxFor(m => m.Items[i].fav_ind)
</td>
<td>
<a href="#" onclick="ShowDeals(#Model.Items[i].item_no);event.returnValue = false; return false;">
DEALS</a>
</td>
<td>#Html.DisplayFor(m => m.Items[i].item_no)
</td>
<td>#Html.DisplayFor(m => m.Items[i].item_desc)
</td>
<td>#Html.DisplayFor(m => m.Items[i].mfr_item)
</td>
<td>#Html.DisplayFor(m => m.Items[i].pack_size)
</td>
<td>#Html.DisplayFor(m => m.Items[i].purc_uom)
</td>
<td>#Html.DisplayFor(m => m.Items[i].purc_uom_conv)
</td>
<td>#Html.DisplayFor(m => m.Items[i].list_prc)
</td>
<td>
#Html.TextBoxFor(m => m.Items[i].nett_prc)
</td>
<td>
#Html.TextBoxFor(m => m.Items[i].Qty)
</td>
</tr>
}
This #for is inside a Html.BeginForm, because I want the user to be able to edit the last 2 fields (nett_prc and qty. There is also a submit bottom on the bottom of this grid(table).
Now for the question, when I submit, I get all the rows back in the controller, but on the items only the 2 fields where is is Html.TextBoxFor() has data in it. I want to get all the fields in the controller. I kwno I can use #Html.HiddenFor(), but I want to be able to display the other fields in labels, and when the user submits I want the values ass well.
Thanks in advance.
You can do both DisplayFor and HiddenFor at the same time:
<td>
#Html.DisplayFor(m => m.Items[i].item_no)
#Html.HiddenFor(m => m.Items[i].item_no)
</td>
However, why would you need HiddenFor? If the user is not supposed to edit some of the values, you can't trust the values that come from the form anyway. And if you can't trust them you will have to read the originals (e.g. from a database). What does HiddenFor buy you here?