MVC (newbie question): How do you set the default value for a View during a Create action?
Questions:
- How do you set default values on a Model when that default value is based on the User.Identity.Name?
- How do you make a field read-only on a View but still have it work as intended at the Model level
Below is my attempt at both of those issues.
Background:
Each Account can create a bunch of Users. I want the Account holders to be able to create Users, but I want the Account value to default to their AccountName (unchangeable). The model will also be used by Admin Accounts, who is able to set the AccountName, so I want to keep that variable on the Model and Views.
Right now my model takes in an optional AccountName param so that I can set it to the User.Identity.Name:
public partial class User
{
public User()
{
this.Games = new HashSet<Game>();
}
public User(string AccountName)
{
this.AccountName = AccountName;
this.Games = new HashSet<Game>();
}
[Required]
public string Name { get; set; }
[Required]
[Display(Name = "Account Name")]
public string AccountName { get; set; }
public string Rank { get; set; }
public virtual ICollection<Game> Games { get; set; }
}
The controller then sets the default value for the new user on Create:
// GET: /User/Create
public ActionResult Create()
{
User user = new User(User.Identity.Name);
return View(user);
}
On the View, I changed the @Html.EditorFor to @Html.DisplayFor since I don't want the users to edit that field:
<div class="editor-label">
@Html.LabelFor(model => model.AccountName)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.AccountName)
@Html.ValidationMessageFor(model => model.AccountName)
</开发者_运维问答div>
The problem is AccountName is a required field, so when I try to save, it will throw a validation error telling me it's required. I can easily keep that field as an EditorFor field and make that field readonly through javascript, but I wanted to know how this is suppose to be done.
Here are a couple thoughts:
Is AccountName really required by the user? I would say it is not since you are setting it in your controller. Thus remove the [Required] attribute and add the User user = new User(User.Identity.Name); to the action method that is handling the POST and that solves that problem. Alternatively you could use @Html.HiddenFor(x => x.AccountName) but that leaves you vulnerable to someone changing the value using firebug or some other tool. In that case you could encrypt/decrypt the value, but seems easier to just manually set it in the post method and you don't have to worry as much about the security of someone maliciously changing the value.
To display the value you could just use @Model.AccountName rather than the lambda
[HttpPost] public ActionResult Create(User model) { if(!ModeState.IsValid) return View(model); model.AccountName = User.Identity.Name; //Probably want to do some check to make sure this isn't null; // Create User in the system here... return View(); // Send them to another page }
精彩评论