1

In one of my strongly typed Views, of type ProfileModel, I invoke an editor for another type:

@model MVC3App.WebUI.Models.ProfileModel

<div class="contact-form">
    @Html.EditorFor(x => x.ContactModel)
</div>

I have this simple model:

public class ContactModel
{
    [Required]
    [Display(Name = "Name:")]
    public string Name { get; set; }

    [Required]
    [Display(Name = "Email:")]
    public string Email { get; set; }

    [Required]
    [Display(Name = "Message:")]
    public string Message { get; set; }
}

And this is what I use in my EditorTemplate view for ContactModel to display the textbox:

@model MVC3App.WebUI.Models.ContactModel

@Html.TextBoxFor(model => model.Name, new { placeholder = "* Name" })

The generated HTML:

<input data-val="true" 
       data-val-required="The field Name: is required." 
       id="ContactModel_Name" 
       name="ContactModel.Name" 
       placeholder="* Name" type="text" value="">

When POSTed back to the server the value is not binding to the model properties.

[HttpPost]
public ActionResult SendPatientContact(ContactModel model)
{
    if (ModelState.IsValid)
    {
        // model.Name is null. :S

        return RedirectToAction("ThankYou");
    }

    return RedirectToAction("ThankYou");
}

If I manually set the name of the textbox to name="Name", the value is binding correctly to the model property. Any suggestions?

0

1 Answer 1

1

In your situation you should not use @Html.EditorFor method but instead use @{Html.RenderPartial("_ContactPartialView", Model.ContactModel);}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.