在剃刀语法文本框的默认值 [英] Text box default value in Razor syntax

查看:105
本文介绍了在剃刀语法文本框的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    <div class="editor-label">
        @Html.LabelFor(model => model.UserName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.AccountModel.UserName)
        @Html.ValidationMessageFor(model => model.AccountModel.UserName)
    </div>

在此页面加载(查看负载),我想用户名文本框中有其价值与标准ASPNET会员PVC门自动填充。如何分配这个默认值的文本框中。请帮忙。谢谢

On this page load (View load), I would like the Username text box to have its value automatically populated from the standard aspnet membership informtion. How can assign this default value to the text box. Please help. thank you

推荐答案

在你的控制器动作,你可以填充视图模型,并在其上​​设置相应的属性。因此,假如你的看法是强类型为 MyViewModel

In your controller action you could populate the view model and set the corresponding properties on it. So assuming your view is strongly typed to MyViewModel:

[Authorize]
public ActionResult Foo()
{
    var model = new MyViewModel
    {
        UserName = User.Identity.Name
    };
    return View(model);
}

和在视图中简单:

<div class="editor-label">
    @Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.UserName)
    @Html.ValidationMessageFor(model => model.UserName)
</div>

如果您的视图模型具有一定的 AccountModel 属性,你必须实例化,并在控制器动作来填充它。在我的例子我已经夷为平地视图模型。

If your view model has some AccountModel property, you will have to instantiate and populate it in the controller action. In my example I have flattened the view model.

这篇关于在剃刀语法文本框的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆