开发者

mvc3 EditForModel

开发者 https://www.devze.com 2023-04-06 12:36 出处:网络
I\'m trying to get the EditFor or/and the EditForModel to work in mvc3, but it does not render anything on the page.

I'm trying to get the EditFor or/and the EditForModel to work in mvc3, but it does not render anything on the page.

I can get LabelFor and TextFor to work, but not the model level stuff. Any help would be great.

My controller event is this:

 public override ViewResult Index()
        {
            var mystate = new WelcomeModel();
            return View(mystate);
        }

Here is my viewmodel:

public class WelcomeModel
    {

        [Display(Name = "Email", ResourceType = typeof(Addresses))]  //resource file value
        [DataType(DataType.EmailAddress)]
        public Email RecoverEmail { get; set; }

        /// <summary>
        /// Holds a list of attributes to the display elements
        /// </summary>
        public IDictionary<string, object> htmlAttrbutes = new Dictionary<string, object>();

        public WelcomeModel()
        {
            RecoverEmail = new Email();
            htmlAttrbutes.Add("class", "largeText");
        }
    }

Here is my view itself:

@model IdentityManager.Models.WelcomeModel

<h2>@ViewBag.Title</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSumma开发者_如何学JAVAry(true)
    <fieldset class="nolabel">

        <div class="row">
            <div class="col">
               @Html.EditForModel()
            </div>
        </div>
    </fieldset>
    <div id="FormSubmitDiv" class="buttonBar" runat="server">
        <input type="submit" value="Save" />
    </div>
}


Your view model has a property called RecoverEmail which itself is a complex type Email. This is not supported automatically by the default editor templates (they recourse into complex sub-types). Brad Wilson explains this scenario. Another possibility is to write a custom editor template for the Email class (~/Views/Shared/EditorTemplates/Email.cshtml) where you can personalize what you need:

@model Email
<div>
    @Html.LabelFor(x => x.Address)
    @Html.EditorFor(x => x.Address)
    @Html.ValidationMessageFor(x => x.Address)
</div>
... some other fields
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号