2016-01-22 44 views
0

我有一個註冊表格(我正在與ASP.NET MVC),我試圖解析每個字段的內容到我的數據庫,但我真的不知道如何做這個。把數據從一個註冊表格數據庫

首先,我不知道應該把數據庫連接到哪裏。它應該在我的註冊控制器內嗎?對不起,如果它聽起來有點傻,但我是所有這些東西的新手!下面的代碼是從我的註冊表單視圖

@model Client.Models.RegisterModel 
@{ 
    ViewBag.Title = "Register"; 
} 

@using (Html.BeginForm("Register", "Register", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
{ 
    @Html.AntiForgeryToken() 
    <h4>Create a new account.</h4> 
    <hr /> 
    @Html.ValidationSummary("", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.Name, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Surname, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.Surname, new { @class = "form-control" }) 
     </div> 
    </div> 
     <div class="form-group"> 
      @Html.LabelFor(m => m.Number, new { @class = "col-md-2 control-label" }) 
      <div class="col-md-10"> 
       @Html.TextBoxFor(m => m.Number, new { @class = "form-control" }) 
      </div> 
     </div> 
      <div class="form-group"> 
       @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" }) 
       <div class="col-md-10"> 
        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) 
       </div> 
      </div> 
       <div class="form-group"> 
        @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) 
        <div class="col-md-10"> 
         @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) 
        </div> 
       </div> 
        <div class="form-group"> 
         @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) 
         <div class="col-md-10"> 
          @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) 
         </div> 
        </div> 
        <div class="form-group"> 
         @Html.LabelFor(m => m.address, new { @class = "col-md-2 control-label" }) 
         <div class="col-md-10"> 
          @Html.TextBoxFor(m => m.address, new { @class = "form-control" }) 
         </div> 
        </div> 
        <div class="form-group"> 
         @Html.LabelFor(m => m.postcode, new { @class = "col-md-2 control-label" }) 
         <div class="col-md-10"> 
          @Html.TextBoxFor(m => m.postcode, new { @class = "form-control" }) 
         </div> 

        </div> 
         <div class="form-group"> 
          @Html.LabelFor(m => m.female, new { @class = "col-md-2 control-label" }) 
          <div class="col-md-10"> 
           @Html.CheckBoxFor(m => m.female, new { @class = "col-md-2 control-label" }) 
          </div> 

         </div> 
         <div class="form-group"> 
          @Html.LabelFor(m => m.male, new { @class = "col-md-2 control-label" }) 
          <div class="col-md-10"> 
           @Html.CheckBoxFor(m => m.male, new { @class = "col-md-2 control-label" }) 
          </div> 

         </div> 
         <div class="form-group"> 
          <div class="col-md-offset-2 col-md-10"> 
           <input type="submit" class="btn btn-default" value="Υποβολή" /> 
          </div> 
         </div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 
+1

這是一個基本問題,你應該對此做一些研究,例如 - http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing -basic-crud-functional-with-the-entity-framework-in-asp-net-mvc-application – ramiramilu

回答

0

目前你是表單的action屬性值設置爲「註冊/註冊」。這意味着當您提交表單時,表單將被髮布到RegisterController中的HttpPost註冊操作方法。由於您已經有了一個用於在表單中呈現輸入字段的類,因此可以將其用作操作方法的參數類型,以便在發佈表單時默認模型聯編程序可以將表單數據映射到該類的一個對象的屬性。

所以你的HttpPost方法看起來就像

[HttpPost] 
public ActionResult Register(RegisterModel model) 
{ 
    var name = model.Name; 
    // to do : Save and redirect 
} 

現在,保存數據,您可以讀取模型對象(例如:model.Name)的不同屬性值,並用它來插入到你的數據庫。有很多不同的選項可以使用純ado.net代碼with SqlConnection and ExecuteNonQuery method或使用ORM庫,如Entity framework或Hibernate等。請記住,選擇是你的。 有沒有這樣的規則說,你應該總是使用實體框架與您的MVC應用

它應該在我的註冊控制器內嗎?

你會看到很多示例/教程有控制器內的代碼保存數據到數據庫。該代碼將工作。這是一個好習慣嗎?可能不會 !將數據訪問代碼移動到單獨的類/層/項目並從需要的地方調用它總是一個好主意。

如果你是一個MVC的新手和將數據保存到db的概念。我建議簡單地把你的代碼放在你的控制器本身(就像那些教程一樣)並使其工作。一旦你成功了,你可以考慮通過移動到single responsibility principle之後的另一個類/層來重構它。

+1

你是非常有幫助的謝謝了很多! –