Skip to content

Instantly share code, notes, and snippets.

@stormwild
Created February 27, 2012 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stormwild/1927312 to your computer and use it in GitHub Desktop.
Save stormwild/1927312 to your computer and use it in GitHub Desktop.
Model
@model MyAppDomain.Entities.Role
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Role</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<p>
<input class="nice radius button" type="submit" value="Create" />
</p>
</fieldset>
}
<p>@Html.ActionLink("Back to List", "Index")</p>
<h2>Create</h2>
<form action="/MyApp/Role/Create" method="post">
<fieldset>
<legend>Role</legend>
<div class="editor-label">
<label for="Name">Name</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-required="The Name field is required."
id="Name" name="Name" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true">
</span>
</div>
<p>
<input class="nice radius button" type="submit" value="Create" />
</p>
</fieldset>
</form>
<p><a href="/MyApp/Role">Back to List</a></p>
namespace MyAppDomain.Entities
{
public class Role
{
[HiddenInput(DisplayValue = false)]
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
}
namespace MyApp.Controllers
{
[CustomAuthorize(Roles = "Admin")]
public class RoleController : Controller
{
private IRoleRepository repository;
public RoleController(IRoleRepository roleRepository)
{
repository = roleRepository;
}
public ViewResult Index()
{
return View(repository.Roles);
}
public ViewResult Create()
{
return View(new Role());
}
[HttpPost]
public ActionResult Create(Role role)
{
if (ModelState.IsValid)
{
repository.SaveRole(role);
TempData["success"] = string.Format("{0} saved", role.Name);
return RedirectToAction("Index");
}
return View(role);
}
}
}
namespace MyApp.Models
{
// view model
public class UserCreate
{
[HiddenInput(DisplayValue = false)]
public int Id { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
[Remote("AvailableEmail", "Validation", HttpMethod = "Post", ErrorMessage = "Email exists and is not available.")]
[RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Please enter a valid email address.")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required, Compare("Password", ErrorMessage = "Passwords must match")]
[DataType(DataType.Password)]
public string ConfirmPassword { get; set; }
[Required]
public int RoleId { get; set; }
public IEnumerable<Role> Roles { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment