Skip to content

Instantly share code, notes, and snippets.

@davybrion
Created September 15, 2012 16:02
Show Gist options
  • Save davybrion/3728590 to your computer and use it in GitHub Desktop.
Save davybrion/3728590 to your computer and use it in GitHub Desktop.
code snippets for "Prefixing Input Elements Of Partial Views With ASP.NET MVC" post
public class NameModel
{
[Display(Name = "First name")]
[Required]
public string FirstName { get; set; }
[Display(Name = "Last name")]
[Required]
public string LastName { get; set; }
}
public class AddressModel
{
[Required]
public string Street { get; set; }
[Required]
public int Number { get; set; }
[Display(Name = "Zip code")]
[Required]
public int ZipCode { get; set; }
[Required]
public string City { get; set; }
}
public class PersonModel
{
public PersonModel()
{
Name = new NameModel();
Address = new AddressModel();
}
public NameModel Name { get; private set; }
public AddressModel Address { get; private set; }
[Required]
[MustBeValidEmailAddress(ErrorMessage = "Not a valid email")]
public string Email { get; set; }
}
public static MvcHtmlString EditorForNameModel<TModel>(
this HtmlHelper<TModel> helper, Expression<Func<TModel, NameModel>> getter)
{
return GetPartial(helper, "NamePartial", getter);
}
public static MvcHtmlString EditorForAddressModel<TModel>(
this HtmlHelper<TModel> helper, Expression<Func<TModel, AddressModel>> getter)
{
return GetPartial(helper, "AddressPartial", getter);
}
@using (Html.BeginForm("MyPostAction", "MyController", FormMethod.Post))
{
@Html.ValidationSummary(true)
<p>
@Html.LabelFor(model => model.Name.FirstName)<br />
@Html.TextBoxFor(model => model.Name.FirstName)
@Html.ValidationMessageFor(model => model.Name.FirstName)
</p>
<p>
@Html.LabelFor(model => model.Name.LastName)<br />
@Html.TextBoxFor(model => model.Name.LastName)
@Html.ValidationMessageFor(model => model.Name.LastName)
</p>
<p>
@Html.LabelFor(model => model.Address.Street)<br />
@Html.TextBoxFor(model => model.Address.Street)
@Html.ValidationMessageFor(model => model.Address.Street)
</p>
<p>
@Html.LabelFor(model => model.Address.Number)<br />
@Html.TextBoxFor(model => model.Address.Number)
@Html.ValidationMessageFor(model => model.Address.Number)
</p>
<p>
@Html.LabelFor(model => model.Address.City)<br />
@Html.TextBoxFor(model => model.Address.City)
@Html.ValidationMessageFor(model => model.Address.City)
</p>
<p>
@Html.LabelFor(model => model.Address.ZipCode)<br />
@Html.TextBoxFor(model => model.Address.ZipCode)
@Html.ValidationMessageFor(model => model.Address.ZipCode)
</p>
<p>
@Html.LabelFor(model => model.Email)<br />
@Html.TextBoxFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</p>
<input type="submit" value="Confirm" />
}
<p>
<label for="Name_FirstName">First name</label><br />
<input data-val="true" data-val-required="The First name field is required." id="Name_FirstName" name="Name.FirstName" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Name.FirstName" data-valmsg-replace="true"></span>
</p>
<p>
<label for="Name_LastName">Last name</label><br />
<input data-val="true" data-val-required="The Last name field is required." id="Name_LastName" name="Name.LastName" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Name.LastName" data-valmsg-replace="true"></span>
</p>
<p>
<label for="Address_Street">Street</label><br />
<input data-val="true" data-val-required="The Street field is required." id="Address_Street" name="Address.Street" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Address.Street" data-valmsg-replace="true"></span>
</p>
@model CarShop.Web.Models.NameModel
<p>
@Html.LabelFor(model => model.FirstName)<br />
@Html.TextBoxFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</p>
<p>
@Html.LabelFor(model => model.LastName)<br />
@Html.TextBoxFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</p>
@model CarShop.Web.Models.AddressModel
<p>
@Html.LabelFor(model => model.Street)<br />
@Html.TextBoxFor(model => model.Street)
@Html.ValidationMessageFor(model => model.Street)
</p>
<p>
@Html.LabelFor(model => model.Number)<br />
@Html.TextBoxFor(model => model.Number)
@Html.ValidationMessageFor(model => model.Number)
</p>
<p>
@Html.LabelFor(model => model.ZipCode)<br />
@Html.TextBoxFor(model => model.ZipCode)
@Html.ValidationMessageFor(model => model.ZipCode)
</p>
<p>
@Html.LabelFor(model => model.City)<br />
@Html.TextBoxFor(model => model.City)
@Html.ValidationMessageFor(model => model.City)
</p>
@using (Html.BeginForm("MyPostAction", "MyController", FormMethod.Post))
{
@Html.ValidationSummary(true)
@Html.Partial("NamePartial", Model.Name)
@Html.Partial("AddressPartial", Model.Address)
<p>
@Html.LabelFor(model => model.Email)<br />
@Html.TextBoxFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</p>
<input type="submit" value="Confirm" />
}
@Html.Partial("NamePartial", Model.Name, new ViewDataDictionary
{
TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "Name" }
})
@Html.Partial("AddressPartial", Model.Address, new ViewDataDictionary
{
TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "Address" }
})
@Html.EditorForNameModel(model => model.Name);
@Html.EditorForAddressModel(model => model.Address);
private static MvcHtmlString GetPartial<TRootModel, TModelForPartial>(
HtmlHelper<TRootModel> helper, string partialName, Expression<Func<TRootModel, TModelForPartial>> getter)
{
var prefix = ExpressionHelper.GetExpressionText(getter);
return helper.Partial(partialName, getter.Compile().Invoke(helper.ViewData.Model),
new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = prefix } });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment