MVC(新手问题):如何在创建操作期间设置视图的默认值?

发布于 2024-11-26 06:58:44 字数 1563 浏览 1 评论 0原文

问题:

  1. 当默认值时,如何在模型上设置默认值 基于 User.Identity.Name?
  2. 如何创建一个字段 在视图上只读,但仍然可以在模型上按预期工作 下面

是我对这两个问题的尝试。

背景:

每个Account可以创建一堆User。我希望帐户持有者能够创建用户,但我希望帐户值默认为其帐户名称(不可更改)。该模型也将由能够设置 AccountName 的管理员帐户使用,因此我想将该变量保留在模型和视图上。

现在我的模型接受一个可选的 AccountName 参数,以便我可以将其设置为 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; }
}

然后控制器在创建时为新用户设置默认值:

    // GET: /User/Create

    public ActionResult Create()
    {
        User user = new User(User.Identity.Name);

        return View(user);
    } 

在视图上,我将 @Html.EditorFor 更改为@Html.DisplayFor 因为我不希望用户编辑该字段:

    <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>

问题是 AccountName 是必填字段,所以当我尝试保存时,它会抛出一个验证错误,告诉我它是必填的。我可以轻松地将该字段保留为 EditorFor 字段,并通过 javascript 将该字段设置为只读,但我想知道这是如何完成的。

Questions:

  1. How do you set default values on a Model when that default value
    is based on the User.Identity.Name?
  2. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2024-12-03 06:58:44

这里有一些想法:

  1. 用户真的需要 AccountName 吗?我想说这不是因为你在控制器中设置了它。因此删除 [Required] 属性并添加 User user = new User(User.Identity.Name);处理 POST 并解决该问题的操作方法。或者,您可以使用 @Html.HiddenFor(x => x.AccountName) 但这会让您容易受到有人使用 firebug 或其他工具更改值的影响。在这种情况下,您可以加密/解密该值,但在 post 方法中手动设置它似乎更容易,并且您不必太担心有人恶意更改该值的安全性。

  2. 要显示值,您可以使用 @Model.AccountName 而不是 lambda

    <前><代码>[HttpPost]
    public ActionResult Create(用户模型)
    {
    if(!ModeState.IsValid)
    返回视图(模型);

    model.AccountName = User.Identity.Name; //可能想要做一些检查以确保这不为空;

    // 在系统中创建用户...
    返回视图(); // 将它们发送到另一个页面
    }

Here are a couple thoughts:

  1. 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.

  2. 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 
     }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文