2014-02-12 26 views
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> 

回答

2

你需要试行方法POST动作像如下:

[HttpPost] 
    public ActionResult Trial(UserAccount model) 
    { 
     if (ModelState.IsValid) 
     { 
      //Store the form data into your database 
     } 
     return View(model); 
    } 

然后在您的视图中,添加一个提交按钮元素的表单中,也代替Edit,你只需要使用Trial为回发。

@using (Html.BeginForm("Trial", "Home")) { 

    // 
    <input type="submit" value="Submit"/> 
} 

注意:如果您没有其他原因,则不需要创建其他编辑操作方法。

如果你不知道如何将数据保存到数据库,下面是一个例子:

创建你的DbContext类

public class MyDbContext : DbContext 
    { 
     public MyDbContext() 
      : base("name=YourDbConnection") 
     { 
     } 

     public DbSet<UserAccount> UserAccounts { get; set; } 
    } 

则动作方法的样子:

public class HomeController : Controller 
{ 
     // 
     [HttpPost] 
      public ActionResult Trial(UserAccount model) 
      { 
       if (ModelState.IsValid) 
       { 
        using (var db = new MyDbContext()) 
        { 
         db.UserAccounts.Add(model); 
         db.SaveChanges(); 
         return RedirectToAction("Index"); 
        } 
       } 
       return View(model); 
      } 
    } 
+0

非常广泛,谢谢!从数据库生成时,我有一个现有的DbContext。我可以使用它吗? – user2471435

+0

hi @ user2471435,当然你可以使用你现有的DbContext。 – Lin

+0

为什么它不识别ModelState?它放在System.Web.Mvc.ModelState中,IsValid方法没有Intellisense? – user2471435

相关问题