Created
December 19, 2014 05:43
MVC EditorFor and data attributes - http://stackoverflow.com/questions/27559582/input-field-set-as-value-instead-of-value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace WebApplication4.Controllers | |
{ | |
// Some POCO's | |
public class DesignParam | |
{ | |
public string ParamID { get; set; } | |
} | |
public class Foo | |
{ | |
public int ID { get; set; } | |
public string Title { get; set; } | |
public DesignParam[] DesignParams { get; set; } | |
} | |
public class HomeController : Controller | |
{ | |
public ActionResult Contact() | |
{ | |
ViewBag.Message = "Your contact page."; | |
var model = new Foo {ID = 1234, Title = "FOO", | |
DesignParams = new [] | |
{ | |
new DesignParam { ParamID = "1234" } // Set the default value ON the actual model | |
}}; | |
return View(model); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model WebApplication4.Controllers.Foo | |
<div class="editor-field"> | |
@Html.Editor("Title", // This is a property of Model - the value is reflected off the property | |
new | |
{ | |
htmlAttributes = new | |
{ | |
@class = "form-control text-right", | |
@type = "text", | |
id = "_" + 54, | |
data_uomid = 9832, | |
data_measureid = 12312 | |
} | |
}) | |
@Html.Editor("DesignParams[0].ParamID", // This doesn't work. | |
new | |
{ | |
htmlAttributes = new | |
{ | |
@class = "form-control text-right", | |
@type = "text", | |
id = "_" + 54, | |
data_uomid = 9832, | |
data_measureid = 12312 | |
} | |
}) | |
@Html.EditorFor(m => m.DesignParams[0].ParamID, // This works, and value is reflected off the property | |
new | |
{ | |
htmlAttributes = new | |
{ | |
@class = "form-control text-right", | |
@type = "text", | |
id = "_" + 54, | |
data_uomid = 9832, | |
data_measureid = 12312 | |
} | |
}) | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment