model-binding

Two fields with the same name

烈酒焚心 提交于 2019-12-24 01:36:09
问题 I have a ViewModel class to encapsulate "Personal" and "Business" models. My problem is that both models have a property called "Email" and the model binding is not able to make a distinction between the two. I read that [Bind(Prefix = ... is used to resolved this issue, but I have not been able to see a concise example on how to achieve this. public class BusinessFormViewModel { public Business Business { get; set; } public ContactPerson ContactPerson { get; set; } public BusinessFromView

MVC Multiple forms in PartialView returns null on postback for except the 1st one listed

◇◆丶佛笑我妖孽 提交于 2019-12-24 00:58:54
问题 Ok, I hope someone out there can help me out on this one (Thanks in advance!). I'm using MVC4. My ContractsModel contains (among other things) a list of ContractModel(s). In my partial view, I loop though the list creating a form for each. So far so good. The issue is on postback. The 1st form always posts back fine. But the others (2, 3, 4, etc) always return null. I've checked with debugging tools and confirmed that the rendered controls are appropriately named. <select name="Contracts[0]

ASP.NET MVC - Custom Model Binder for ID fields

与世无争的帅哥 提交于 2019-12-24 00:44:39
问题 i have the following entities: public class Category { public virtual int CategoryID { get; set; } [Required(ErrorMessage = "Section is required")] public virtual Section Section { get; set; } [Required(ErrorMessage = "Category Name is required")] public virtual string CategoryName { get; set; } } public class Section { public virtual int SectionID { get; set; } public virtual string SectionName { get; set; } } Now within my add category view i have a textbox to enter the SectionID eg: <%=

When I need to use POST method with multiple parameters instead of separate model?

為{幸葍}努か 提交于 2019-12-23 11:58:06
问题 Could you tell me when I need to use multiple parameters instead of separate model in my WebApi/MVC application? I have an action which takes a few parameters. [HttpPost] public InfoViewModel GetInfo(IEnumerable<Guid> Ids, DocumentType type) { // to do smth } Also I can transform this action to the following: [HttpPost] public InfoViewModel GetInfo(RequestViewModel model) { // to do smth } I need a special model for the second case. public class RequestViewModel { public IEnumerable<Guid> Ids

How to create an ActionLink with Properties for the View Model

☆樱花仙子☆ 提交于 2019-12-23 10:09:31
问题 I have a ViewModel with a Filter property that has many properties that I use to filter my data Example: class MyViewModel : IHasFilter { public MyData[] Data { get; set; } public FilterViewModel Filter { get; set; } } class FilterViewModel { public String MessageFilter { get; set; } //etc. } This works fine when using my View. I can set the properties of Model.Filter and they are passed to the Controller. What I am trying to do now, is create an ActionLink that has a query string that works

Modelbinding JSON in .Net Core 2.2 Web API

我们两清 提交于 2019-12-23 09:15:56
问题 I just wasted a lot of hours with trying to get a custom ComplexTypeModelBinder to work. Whatever I did, it never worked. As it turns out, this only works when the data is POSTed as form data; when you post a JSON object (in my case from a Swagger "try out" form) the ComplexTypeModelBinder never invokes the SetProperty method. I have a lot of models, some more complex than others, and I have annotated some of the properties with a custom attribute. Whenever that property is bound I want it to

Why am I getting “The modifier 'virtual' is not valid for this item” error?

一笑奈何 提交于 2019-12-23 07:26:32
问题 I'm trying to create mvc application with model below: (the code is large. I think it will be more understandable for you) public class Job { public int JobId { get; set; } public string Name { get; set; } public List<Job> GetJobs() { List<Job> jobsList = new List<Job>(); jobsList.Add(new Job { JobId = 1, Name = "Operator" }); jobsList.Add(new Job { JobId = 2, Name = "Performer" }); jobsList.Add(new Job { JobId = 3, Name = "Head" }); return jobsList; } } public class Person { public virtual

knockoutjs with mvc collection model binding

淺唱寂寞╮ 提交于 2019-12-23 05:39:25
问题 I'm using knockoutjs to render a collection of items. After allowing the user to do some inline editing I need to post the collection back to the server. However, the collection isn't being populated on the server because I'm not using the name="[0].Blah" naming convention. Does anyone know how to either render name attributes like this using knockoutjs OR how to create a model binder that will allow me to extract the values from the ValueProvider? You can see a screenshot of the

How to handle model binding of Complex Type with List of interfaces and nested list of interfaces with possibly no values

北城以北 提交于 2019-12-23 04:36:32
问题 I have successfully(maybe not elegantly) created a model binder that will bind a List of Interfaces on post. Each interface has separate properties and some have a nested List of another interface. The list of interfaces get displayed correctly in the view and so do the nested list items. On post everything works, the custom model binders get called and the correct types get built. The issue that has me stuck is that if a nested List of interfaces has no items to display, on post back the

ASP.Net Model binding - WebForms

不问归期 提交于 2019-12-23 01:41:38
问题 I have been asked to work on an ASP.Net WebForms project and I want to use ModelBinding like I do in ASP.Net MVC. I am having some problems in getting this to work and would like some help if possible. If I want to bind a model to objects on a Form I need to do this in a FormView, GridView and so on but in this instance I want to use a FormView. The below code is a simplified version of what I am working with but everybit relevant. <asp:FormView runat="server" ID="frmMain" ItemType=