C# 在MVC ASP.NET中使用创建视图时录制活动用户

C# 在MVC ASP.NET中使用创建视图时录制活动用户,c#,asp.net-mvc,C#,Asp.net Mvc,基本上,我有一个创建视图,人们可以在其中创建数据库条目,如下所示: @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Trade</legend> <div class="editor-label"> @Html.LabelFor(model => model.Na

基本上,我有一个创建视图,人们可以在其中创建数据库条目,如下所示:

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Trade</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>

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

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
贸易
@LabelFor(model=>model.Name)
@EditorFor(model=>model.Name)
@Html.ValidationMessageFor(model=>model.Name)
@LabelFor(model=>model.Price)
@EditorFor(model=>model.Price)
@Html.ValidationMessageFor(model=>model.Price)
@LabelFor(model=>model.active)
@EditorFor(model=>model.active)
@Html.ValidationMessageFor(model=>model.active)

}
数据库还有一个名为“Name”的字段,我想包含创建条目的用户的名称。我可以使用@user.Identity.Name获取当前用户的用户名,但我不确定如何设置,因此当用户单击“提交”时,该用户名会自动添加到名称字段中


提前感谢您的帮助

您不需要将其填入名称字段。在处理post的控制器中,只需使用:

[HttpPost] 
public ActionResult Create(Trade trade) { 
trade.Name=HttpContext.User.Identity.Name; //this is where the current user is added
if (ModelState.IsValid) { 
db.Movies.Add(trade); 
db.SaveChanges(); 
return RedirectToAction("Index"); 
} 
return View(trade); 
} 

对不起,我不确定我是否理解,我应该把它放在控制器的什么地方?现在我的Create()控制器看起来像:public ActionResult Create(){return View();}///POST:/ActiveTrades/Create[HttpPost]public ActionResult Create(Trade){if(ModelState.IsValid){db.Movies.Add(Trade);db.SaveChanges();返回重定向到操作(“索引”);}返回视图(交易);}