2013-05-06 62 views
0

我沒有一個很好的例子,主要是因爲我只是對它進行了修改,到目前爲止,我的代碼只是一個混亂的gobble-dee-gook混雜的混亂。我很茫然,需要一些幫助或建議。這是我需要的:MVC4使用一個模型創建註冊頁面

我正在創建一個模擬註冊表格僅用於學習目的。作爲我的例子,我使用了工作申請表。一頁有申請人的個人信息,如姓名,年齡,性別和教育程度。第二頁允許他們選擇他們希望申請的職位,並允許他們提供技能列表。我有一個model設置爲將datasave設置爲database。第一頁將有一個ajax下一個按鈕用第二個頁面表單替換第一個頁面表單。第二頁有兩個按鈕,返回提交(很簡單),也是ajax-y。我現在的問題是從兩個表單中獲取data以保存model中的一個entry。有沒有人有一個簡單的例子或鏈接,我可以研究這種情況?或者甚至可能還有其他方法來解決這個問題?這將不勝感激!! :)

型號

public int Id { get; set; } 

    [Required(ErrorMessage = "First name Req")] 
    [Display(Name = "First name")] 
    public string First { get; set; } 

    [Required(ErrorMessage = "Last name Req")] 
    [Display(Name = "Last name")] 
    public string Last { get; set; } 

    [Required(ErrorMessage = "Age Req")] 
    [Range(18, 75, ErrorMessage = "Age Range of {1}-{2}")] 
    public int Age { get; set; } 

    [Required(ErrorMessage = "Please Select Gender")] 
    public string Gender { get; set; } 

    [Required(ErrorMessage = "Education Level Req")] 
    public string Education { get; set; } 

    public string Position { get; set; } 

    public string Skills { get; set; } 

控制器

[HttpGet] 
    public PartialViewResult Apply1() 
    { 
     var model = db.Applications.Find(id); 
     if (model != null) 
     { 
      return PartialView("_Apply1", model); 
     } 
     return PartialView("_Apply1"); 
    } 

    [HttpPost] 
    public PartialViewResult Apply1(Application app) 
    { 
     if (Request.IsAjaxRequest()) 
     { 
      if (db.Applications.Where(a => a.First.ToLower() == app.First.ToLower() && a.Last.ToLower() == app.Last.ToLower() && a.Age == app.Age && a.Gender == app.Gender && a.Education == app.Education).Count() == 0) 
      { 
       app.Position = "x"; 
       db.Applications.Add(app); 
       db.SaveChanges(); 
      } 
      else 
      { 
       app = db.Applications.Single(a => a.First.ToLower() == app.First.ToLower() && a.Last.ToLower() == app.Last.ToLower() && a.Age == app.Age && a.Gender == app.Gender && a.Education == app.Education); 
      } 

      PosList(); //This is a helper Method, get's values for a dropdown list 
      id = app.Id; 
      var model = db.Applications.Find(id); 
      return PartialView("_Apply2", model); 
     } 

     return PartialView("_Apply1", app); 
    } 

    [HttpGet] 
    public PartialViewResult Apply2() 
    { 
     var model = db.Applications.Find(id); 
     if (model != null) 
     { 
      return PartialView("_Apply2", model); 
     } 
     return PartialView("_Apply2"); 
    } 

    [HttpPost] 
    public PartialViewResult Apply2(Application app) 
    { 


     if (ModelState.IsValid) 
     { 
      db.Entry(app).State = EntityState.Modified; 
      db.SaveChanges(); 
      ModelState.Clear(); 
      return PartialView("_Success"); 
     } 

     PosList(); 
     return PartialView("_Apply2", app); 
    } 

首先查看

@model SiPrac.Models.Application 

@using (Ajax.BeginForm("Apply1", new AjaxOptions() 
{ 
InsertionMode = InsertionMode.Replace, 
UpdateTargetId = "appForm" 
})) 
{ 
@Html.AntiForgeryToken() 

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

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

<input class="btn" type="submit" value="Next" /> 
} 

第二視圖

@using (Ajax.BeginForm("Apply2", new AjaxOptions() 
{ 
InsertionMode = InsertionMode.Replace, 
UpdateTargetId = "appForm" 
})) 
{ 
@Html.AntiForgeryToken() 

<div class="editor-label"> 
    @Html.LabelFor(model => model.Position) 
</div> 
<div class="editor-field"> 
    @Html.DropDownListFor(model => model.Position, (IEnumerable<SelectListItem>)ViewData["selectPos"]) 
</div> 
<div class="clear"></div> 

<input class="btn" type="submit" value="Submit" /> 
} 
+0

顯示你所擁有的,你可以包含每個表單中的一個字段,因此您不必在問題中發佈大量代碼。另外,發佈你的模型。但基本上,您可以通過ajax將輸入傳遞到模型中。 – 2013-05-06 12:35:21

+0

我到目前爲止已添加。 – SiSan 2013-05-06 12:51:32

+0

你說過你想要通過這兩種形式的所有字段,但是你在這裏顯示了兩種post方法。那麼你是否打算從這兩種表達方式中的兩種表單中傳遞所有字段 - 雖然沒有意義? – 2013-05-06 13:11:36

回答

1

好吧我會盡量縮短這一點。因此,假設你已經知道如何,形式之間切換,因爲我知道你根據你的問題,你必須形成這樣的:

<form id="form1"> 
    @Html.EditorFor(model => model.First) 
    // rest of the fields goes here 
</form> 
<form id="form2"> 
    @Html.EditorFor(model => model.Position) 
    // rest of the fields goes here 
    <button id="submitAll">Submit</button> 
</form> 

那假設你有按鍵來回切換的意見。該submitAll按鈕觸發回發動作控制器方法,並通過這樣的價值:

function submitAllFields() { 
    var o = { 
     // list properties here similar to your model property name 
     // and assign values to them 
     First: $("#First").val(), 
     Position: $("#Position").val() 
    }; 
    $.post('@Url.Action("apply2")', { app: o }, function(result) { 
     // do something here 
    }); 
} 

然後,您需要一種方法來接受所有輸入:

[HttpPost] 
public PartialViewResult Apply2(Application app) 
{ 
    if (ModelState.IsValid) 
    { 
     db.Entry(app).State = EntityState.Modified; 
     db.SaveChanges(); 
     ModelState.Clear(); 
     return PartialView("_Success"); 
    } 
    // do something here 
} 
+0

所以,在你提供的例子中,快速的問題,表單是在不同的視圖,對嗎?我不確定這是我應該如何設置的,我只是假設他們在單獨的頁面上。 – SiSan 2013-05-06 13:59:22

+0

如果你沒有一百個領域,他們不需要特別注意。 – 2013-05-06 14:05:54

+0

我很抱歉,那是我練習的重點。在單個頁面上有兩種不同的表單很容易,我知道如何使用。但這不是這裏發生的事情。我知道我目前沒有一堆字段,但我需要能夠學習如何將來自兩個單獨頁面上的兩個單獨表單的所有數據保存到單個數據庫條目中。該模型保存要由用戶填寫的那些字段的列表。如果我在引言中沒有解釋得那麼好,我非常抱歉。 D: – SiSan 2013-05-06 14:13:16