model-binding

Model binding for a ViewModel containing multiple objects

北慕城南 提交于 2019-12-13 03:49:10
问题 I have a strongly typed view of type ProductListingViewModel which in turn contains a ProductViewModel. (both custom view models). I have some form elements on my page and these are created like so: <%: Html.DropDownListFor(m => m.ProductViewModel.CategoryId, Model.Categories)%> which generates the HTML: <select name="ProductViewModel.CategoryId" id="CategoryId"> With the default model binding I expected that when I post to my controller action which accepts a parameter of type

How to create an HtmlHelper extension method that will bind an IEnumerable<T> to a table

对着背影说爱祢 提交于 2019-12-12 18:26:36
问题 This is my view model: public class TaskViewModel{ public int TaskID{get;set;} public IEnumerable<TaskExecutor> Executors{get;set;} } public class TaskExecutor{ public int ExecutorID{get;set;} public string LastName{get;set;} public string FirstName{get;set;} } In my view I have done something like this: <table> @foreach(var item in Model.Executors) { <tr> <td>item.ExecutorID</td> <td>@string.Format("{0} {1}",item.FirstName,item.LastName)</td> </tr> } </table> Now, when loading the view,

Add an empty item to a bounded combobox

孤人 提交于 2019-12-12 16:08:14
问题 I need to add an empty item in a bounded combobox within wpf mvvm application, I tried this <ComboBox TabIndex="23" Text="{Binding Specialisation}" DisplayMemberPath="refsp_libelle"> <ComboBox.ItemsSource> <CompositeCollection > <ComboBoxItem > </ComboBoxItem> <CollectionContainer Collection="{Binding SpecialisationItemSource}" ></CollectionContainer> </CompositeCollection> </ComboBox.ItemsSource> </ComboBox> It worked before I tried to add the empty item. <ComboBox TabIndex="23" Text="

MVC action parameter inconsistently binds to nullable Guid

风格不统一 提交于 2019-12-12 16:00:58
问题 This really seems like a framework bug. The parameter is in the request's params with correct name but it doesnt always bind to the action parameter. It worked for 6 months, but now this is happening in several action methods across the application. I was able to either close VS or restart computer and that would usually fix it. Lately when i've run into this i couldnt get by it without turning the paramter into a string and then converting to a GUID. Any advice on what to do with this since

mvc6 custom model binder not firing

前提是你 提交于 2019-12-12 15:12:10
问题 I'm trying to figure out model binding in current mvc6 (visual studio 2015 release candidate). This is what my code looks like so far: public class MyObjectModelBinder : IModelBinder { public Task<ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext) { if (bindingContext.ModelType == typeof(MyObject)) { var model = new MyObject(); return Task.FromResult(new ModelBindingResult(model, bindingContext.ModelName, true)); } return Task.FromResult<ModelBindingResult>(null); } } The

MVC Model Binding with Dynamic Collection

强颜欢笑 提交于 2019-12-12 12:02:54
问题 According to the seminal Scott Hanselman article on the complexities of the ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries: We read in the properties by looking for parameterName[index].PropertyName The index must be zero-based and unbroke So this HTML: <input type="text" name= "People[0].FirstName" value="George" /> <input type="text" name= "People[1].FirstName" value="Abraham" /> <input type="text" name= "People[2].FirstName" value="Thomas" /> Which will

Model binding a collection property with a partial view

纵然是瞬间 提交于 2019-12-12 10:49:10
问题 Suppose I have a model like this: public class Foo { public List<Bar> Bars { get; set; } public string Comment { get; set; } } public class Bar { public int Baz { get; set; } } And I want a view of Foo that lets users edit Bar items. But with a catch: I want the Bar editing to be handled by a partial view. @model Web.ViewModels.Foo @using(Html.BeginForm()) { @Html.Partial("_EditBars", Model.Bars) @Html.TextAreaFor(m => m.Comment) ... } The _EditBars partial view looks something like: @model

MVC3 - HiddenFor Dictionary Values

牧云@^-^@ 提交于 2019-12-12 09:51:26
问题 I have a dictionary property called Week: public IDictionary<DayOfWeek, Day> Week { get; private set; } And I'm trying to pass its values off to HiddenFor (Days) @Html.HiddenFor(x => x.Week.Values) It has to be a property of the Model so I can't do x.Week.Values.ToList(); How would I go about passing the Dictionary Values to the Html.HiddenFor ? 回答1: Well since your using HiddenFor , I'm going to assume that you need to rebind the values of the dictionary on form post. To bind a Dictionary to

CultureInfo issue with Modelbinding double in asp.net-mvc(2)

自作多情 提交于 2019-12-12 08:08:09
问题 In my Jquery script I post two doubles using the browser's CultureInfo (en-UK) that uses the . as a fraction separator. My MVC app is running on a server with locale nl-BE using the , as a fraction separator. [AcceptVerbs(HttpVerbs.Post)] public JsonResult GetGridCell(double longitude, double latitude) { var cell = new GridCellViewModel { X = (int)Math.Round(longitude, 0), Y = (int)Math.Round(latitude, 0) }; return Json(cell); } The modelbinding fails because of the parsing issue. I think it

.NET 4.5 WebForms: do I (still) really have to specify all 3 templates in a FormView?

帅比萌擦擦* 提交于 2019-12-12 08:06:24
问题 Investigating the new strongly-typed, model-binding approach within ASP.NET 4.5 WebForms: In Scott Hanselman's example of WebForms model binding (amongst others) I've seen the use of a FormView that opens in "Edit" mode, containing a number of DynamicControls e.g. <asp:FormView runat="server" ID="MyForm" ... DefaultMode="Edit"> <EditItemTemplate> <asp:DynamicControl runat="server" ID="Field1" DataField="Field1" Mode="Edit" /> <asp:DynamicControl runat="server" ID="Field2" DataField="Field2"