0
原谅新手ASP.NET MVC问题。我习惯于Code First与实体框架一起使用的教程。在这里,情况并非如此。我有一个表单,我希望用户填写。当它被填写完毕后,我想用EF来将值写入现有数据库。我不知道如何“陷入”视图中的值,所以我可以写我的EF代码。我使用了一个模型,并将BeginForm重定向到了“编辑”操作方法,但我不知道如何让我的课程充满。这里是HomeController的方法:在现有数据库中存储查看表单
[HttpGet]
public ActionResult Trial()
{
UserAccount account = new UserAccount();
return View(account);
}
public ActionResult Edit()
{
}
下面是模型类:
这是向导生成的视图。当我点击“创建”按钮时,我想要进入“编辑”操作菜单或某处我可以使用EF写入现有的数据库表。我该怎么做呢?
@model AlphaFrontEndService.Models.UserAccount
@{
ViewBag.Title = "Trial";
}
<h2>Trial</h2>
@using (Html.BeginForm("Edit", "Home"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>UserAccount</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.AccountID, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AccountID)
@Html.ValidationMessageFor(model => model.AccountID)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AccountName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AccountName)
@Html.ValidationMessageFor(model => model.AccountName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RegistrationCode, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RegistrationCode)
@Html.ValidationMessageFor(model => model.RegistrationCode)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Created, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Created)
@Html.ValidationMessageFor(model => model.Created)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
非常广泛,谢谢!从数据库生成时,我有一个现有的DbContext。我可以使用它吗? – user2471435
hi @ user2471435,当然你可以使用你现有的DbContext。 – Lin
为什么它不识别ModelState?它放在System.Web.Mvc.ModelState中,IsValid方法没有Intellisense? – user2471435