Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @model WebApplication1.Controllers.PersonViewModel
- @{
- ViewBag.Title = "Index";
- }
- <h1>@ViewBag.Title</h1>
- @using(Html.BeginForm())
- {
- @Html.LabelFor(x => x.FirstName)
- @Html.TextBoxFor(x => x.FirstName, new { @class = "form-control" })
- @Html.LabelFor(x => x.LastName)
- @Html.PasswordFor(x => x.LastName, new { @class = "form-control" })
- @Html.LabelFor(x => x.Address.City)
- @Html.TextBoxFor(x => x.Address.City, new { @class = "form-control" })
- @Html.LabelFor(x => x.Address.Number)
- @Html.TextBoxFor(x => x.Address.Number, new { @class = "form-control" })
- @Html.LabelFor(x => x.Address.StateId)
- @Html.DropDownListFor(x => x.Address.StateId,
- Model.States?.Select(x => new SelectListItem
- { Text = x.Name, Value = x.Id.ToString() }), "--Wybierz--",
- new { @class = "form-control" })
- <button class="btn btn-primary">Klik</button>
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace WebApplication1.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- var model = new PersonViewModel
- {
- FirstName = "Jan",
- };
- InternalIndex(model);
- return View(model);
- }
- [HttpPost]
- public ActionResult Index(PersonViewModel p)
- {
- InternalIndex(p);
- return View(p);
- }
- private PersonViewModel InternalIndex(PersonViewModel p)
- {
- p.States = new[]
- {
- new StateItem(1, "śląskie"),
- new StateItem(2, "mazowieckie"),
- new StateItem(3, "małopolskie"),
- new StateItem(4, "pomorskie"),
- new StateItem(5, "warmińsko-mazurskie")
- };
- return p;
- }
- }
- public class PersonViewModel
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public AddressViewModel Address { get; set; }
- public IEnumerable<StateItem> States { get; set; }
- }
- public class StateItem
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public StateItem()
- {
- }
- public StateItem(int id, string name)
- {
- Id = id;
- Name = name;
- }
- }
- public class AddressViewModel
- {
- public string City { get; set; }
- public int Number { get; set; }
- public int StateId { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement