model-binding

MVC3 ModelBinding to a collection posted back with index gaps

*爱你&永不变心* 提交于 2019-12-01 06:20:36
I have a collection of objects on my Model that I'm rendering in a View by using EditFor function, and I have an EditorTemplate which is responsible for actually rendering each object. @Html.EditorFor(model => model.MyObjects) This has worked well for a while now, and when you check the html, my text boxes are prefixed with the model property, followed by the index of the collection they came from. <input class="text-box single-line" id="MyObjects_2__SomeProperty" name="MyObjects[2].SomeProperty" type="Text" value="" /> However I've recently started using the ShowForEdit and ShowForDisplay

MVC Custom type property is not binding in my model

老子叫甜甜 提交于 2019-12-01 05:29:18
问题 I have the following scenario: A C# base project, with all data domains (custom types) that are used by all others projects in the company. So, It´s a little bit hard to modify it. Now, we are creating our first mvc project with that base project as reference, and the model binding doesnt work for properties of those strings custom types: [Serializable] [TypeConverter(typeof(ShortStrOraTypeConverter))] public class ShortStrOra : BaseString { public ShortStrOra() : this(String.Empty) { }

How to map a 1 or 0 in an ASP.Net MVC route segment into a Boolean action method input parameter

给你一囗甜甜゛ 提交于 2019-12-01 04:53:17
We have some PHP and Javascript apps that call into some ASP.NET MVC endpoints. Let's say we have this endpoint: public ActionResult DoSomething(bool flag) { } I want it to match the value for flag whether I pass in an integer of 1 or 0, or pass in a string of "true" or "false". What part of the framework do I need to implement in order to match that up? The best way to do this is using a custom value provider. While you can do this using a complete custom model binder, that is overkill based on your requirements, and it is much easier simply to implement a custom value provider. For some

How to bind posted data named “file[]” to an MVC model?

吃可爱长大的小学妹 提交于 2019-12-01 04:18:29
I am using Redactor as an HTML editor, which has a component for uploading images and files . Redactor takes care of the client side bit, and I need to provide the server side upload functionality. I have no problem getting the uploads to work if I use Request.Files in the controller. But I would like to bind the posted files to a Model, and I seem unable to do this, because the parameter they are sent with is files[] - with square brackets in the name. My question: Is it possible to bind the posted "file[]" to an MVC model? It's an invalid property name, and using file alone doesn't work.

MVC 3 AJAX Post, List filled with Objects, but Objects Properties are Empty

若如初见. 提交于 2019-12-01 03:53:34
I have the following problem: On a Button-Click I POST some data to the server. My controller Action looks like this: public ActionResult Accept(List<MyViewModel> entries) { //here entries HAS 2 MyViewModel-Instances in it. //The entries are not null, but the values of the instances are! //entries[0].ParamA is null } Where the MyViewModel looks like this: public class MyViewModel { public string ParamA { get; set; } public string ParamB { get; set; } } And the AJAX-Call is the follwing: var myEntries = { entries: [{ ParamA: "A", ParamB: "B" }, { ParamA: "C", ParamB: "D" }] }; $.ajax({ type:

is it possible to have modelbinding in asp.net webapi with uploaded file?

匆匆过客 提交于 2019-12-01 03:30:32
the model: public class UploadFileModel { public int Id { get; set; } public string FileName { get; set; } public HttpPostedFileBase File { get; set; } } the controller: public void Post(UploadFileModel model) { // never arrives... } I am getting an error "No MediaTypeFormatter is available to read an object of type 'UploadFileModel' from content with media type 'multipart/form-data'." Is there anyway around this? It's not easily possible. Model binding in Web API is fundamentally different than in MVC and you would have to write a MediaTypeFormatter that would read the stream of files into

How to bind posted data named “file[]” to an MVC model?

让人想犯罪 __ 提交于 2019-12-01 02:04:54
问题 I am using Redactor as an HTML editor, which has a component for uploading images and files. Redactor takes care of the client side bit, and I need to provide the server side upload functionality. I have no problem getting the uploads to work if I use Request.Files in the controller. But I would like to bind the posted files to a Model, and I seem unable to do this, because the parameter they are sent with is files[] - with square brackets in the name. My question: Is it possible to bind the

How to map a 1 or 0 in an ASP.Net MVC route segment into a Boolean action method input parameter

烂漫一生 提交于 2019-12-01 01:09:34
问题 We have some PHP and Javascript apps that call into some ASP.NET MVC endpoints. Let's say we have this endpoint: public ActionResult DoSomething(bool flag) { } I want it to match the value for flag whether I pass in an integer of 1 or 0, or pass in a string of "true" or "false". What part of the framework do I need to implement in order to match that up? 回答1: The best way to do this is using a custom value provider. While you can do this using a complete custom model binder, that is overkill

Non sequential list binding not working

一世执手 提交于 2019-11-30 23:59:09
As per this article I am trying to bind a list of non sequential items. View: <%using (Html.BeginForm("Products", "Home", FormMethod.Post)) { %> <input type="hidden" name="products.Index" value="cold" /> <input type="text" name="products[cold].Name" value="Beer" /> <input type="text" name="products[cold].Price" value="7.32" /> <input type="hidden" name="products.Index" value="123" /> <input type="text" name="products[123].Name" value="Chips" /> <input type="text" name="products[123].Price" value="2.23" /> <input type="hidden" name="products.Index" value="caliente" /> <input type="text" name=

How do I ModelBind a many-to-many relationship with MVC 3 and Entity Framework Code First?

空扰寡人 提交于 2019-11-30 23:46:01
I'm coming across the same problem in my MVC 3 applications. I've got a view to create an new product and that product can be assigned to one or more categories. Here are my EF Code First Model Classes: public class Product { public int ProductID { get; set; } public string Name { get; set; } public virtual ICollection<Category> Categories { get; set; } } public class Category { public int CategoryID { get; set; } public string Name { get; set; } public virtual ICollection<Product> Products { get; set; } } So, I create a view model for the create product view and include the product and a list