Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created November 5, 2018 05:49
Show Gist options
  • Save danielplawgo/c44f05a8fced524be355b180f7c65406 to your computer and use it in GitHub Desktop.
Save danielplawgo/c44f05a8fced524be355b180f7c65406 to your computer and use it in GitHub Desktop.
Dlaczego Editor Template jest lepsze niż Partial View do tworzenia formularzy?
@model PartialForms.ViewModels.Shared.AddressViewModel
<div class="form-group">
@Html.LabelFor(model => model.Street, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Street, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Street, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Number, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Number, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Number, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Postal, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Postal, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Postal, "", new { @class = "text-danger" })
</div>
</div>
public class AddressViewModel
{
public string Street { get; set; }
public string Number { get; set; }
public string City { get; set; }
public string Postal { get; set; }
}
@model PartialForms.ViewModels.Users.UserViewModel
@{
ViewBag.Title = "EditorTemplate";
}
<h2>EditorTemplate</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>UserViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
@Html.EditorFor(model => model.Address)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@model PartialForms.ViewModels.Users.UserViewModel
@{
ViewBag.Title = "Partial";
}
<h2>Partial</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>UserViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
@Html.Partial("Address", Model.Address)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
public class UsersController : Controller
{
public ActionResult Partial()
{
return View(new UserViewModel());
}
[HttpPost]
public ActionResult Partial(UserViewModel viewModel)
{
return View(viewModel);
}
public ActionResult EditorTemplate()
{
return View(new UserViewModel());
}
[HttpPost]
public ActionResult EditorTemplate(UserViewModel viewModel)
{
return View(viewModel);
}
}
public class UserViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public AddressViewModel Address { get; set; } = new AddressViewModel();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment