2012-08-29 201 views
1

因此,我有這個Property類,它由IDNameDataType定義。 DataType已經填充了靜態值,並且正在用作下拉列表。在模型中保存模型

現在,當用戶從列表中選擇一定的值,List的值是準確的,應用程序將打開附加文本框和按鈕,以填充該列表。

模型是這樣的。

房產模型

public class Property 
{ 
    public int ID {get; set;} 
    public string Name {get; set;} 

    public int DTypeID {get; set;} 
    public virtual DType DTypes {get; set;} 
} 

列表模型

public class DList 
{ 
    public int ID {get; set;} 
    public int PropertyID {get; set;} 
    public string ListValue {get; set;} 
} 

這是我到目前爲止已經完成。

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <div class="labels tableRow"> 
      <div class="editor-label tableCell"> 
       <p> 
        Name 
       </p> 
       <p> 
        Data Type 
       </p> 
      </div> 
      <div class="editor-field tableCell"> 
       <p> 
        @Html.TextBoxFor(model => model.Name, new { @class = "textEntry" }) 
        @Html.ValidationMessageFor(model => model.Name) 
       </p> 
       <p> 
        @Html.DropDownList("DTypeID", (SelectList)ViewBag.DTypeID, new { @class = "dropdown", @onchange = "DropDownChange()"}) 
        @Html.ValidationMessageFor(model => model.DTypeID) 
       </p> 
      </div> 
     </div> 


     <div id="StaticListUI" class="invis"> 
      <div class="labels tableRow"> 
       <div class="editor-label tableCell"> 
        <p> 
         @*here goes list*@ 
        </p> 
       </div> 
       <div class="editor-field tableCell"> 
        <p> 
         @*here goes textbox*@ 
         @Html.TextBox("textboxList") 
        </p> 
        <p> 
         @*here goes button*@ 
         <button name="button" value="add">Add</button> 
        </p> 
       </div> 

      </div> 

     </div> 


     <p class="labels"> 
      @*<input type="submit" value="Create" />*@ 
      <button name="button" value="create">Create</button> | 
      <input type="button" value="Back" onClick='javascript:location.href = "@Url.Action("Index", "Property")";' /> 
     </p> 
    </fieldset> 
} 

所以爲了捕獲哪個按鈕被點擊,我爲我的控制器做了這個。

[HttpPost] 
public ActionResult Create(string button, string textboxList, Property property) 
{ 

    if (button == "add") 
    { 
     var lastProperty = db.Properties.OrderByDescending(p => p.PropertyID).FirstOrDefault(); 
     int propID; 
     if (lastProperty == null) 
     { 
      propID = 1; 
     } 
     else 
     { 
      propID = 1 + lastProperty.PropertyID; 
     } 

     DList dList = new DList(); 

     dList.PropertyID = propID; 
     dList.ListValue = textboxList; 

     db.DLists.Add(dList); 

     return View(property); 
    } 

    string projectID = System.Web.HttpContext.Current.Session["_SelectedProjectID"].ToString(); 
    int projID = Convert.ToInt32(projectID); 
    property.ProjectID = projID; 
    property.DateCreated = DateTime.Now; 
    property.DateEdited = DateTime.Now; 

    if (ModelState.IsValid) 
    { 
     db.Properties.Add(property); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    ViewBag.DTypeID = new SelectList(db.DType, "ID", "Name", property.DTypeID); 
    return View(property); 
} 

而問題是,當我點擊Add按鈕,它仍然發出支票ModelState.IsValid,這不應該。我做了什麼錯了,還是我做任何事情的權利:(

注:其他一切工作的基本

編輯

所以我改變了從控制器按鈕,點擊不同的接受參數但還是缺了點什麼......

[ActionName("Create")] 
    [AcceptVerbs(HttpVerbs.Post)] 
    [AcceptParameter(Name = "button", Value = "add")] 
    public ActionResult Create_Add(string textboxList) 
    { 
     var lastProperty = db.Properties.OrderByDescending(p => p.PropertyID).FirstOrDefault(); 
     int propID; 
     if (lastProperty == null) 
     { 
      propID = 1; 
     } 
     else 
     { 
      propID = 1 + lastProperty.PropertyID; 
     } 

     DList dList = new DList(); 

     dList.PropertyID = propID; 
     dList.ListValue = textboxList; 

     db.DLists.Add(dList); 
     db.SaveChanges(); 



     return View(); 
    } 

    [AcceptVerbs(HttpVerbs.Post)] 
    [AcceptParameter(Name="button", Value="create")] 
    public ActionResult Create(Property property) 
    { 
     string projectID = System.Web.HttpContext.Current.Session["_SelectedProjectID"].ToString(); 
     int projID = Convert.ToInt32(projectID); 
     property.ProjectID = projID; 
     property.DateCreated = DateTime.Now; 
     property.DateEdited = DateTime.Now; 

     if (ModelState.IsValid) 
     { 
      db.Properties.Add(property); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.DTypeID = new SelectList(db.DType, "ID", "Name", property.DTypeID); 
     return View(property); 
    } 


    public class AcceptParameterAttribute : ActionMethodSelectorAttribute 
    { 
     public string Name { get; set; } 
     public string Value { get; set; } 

     public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo) 
     { 
      var req = controllerContext.RequestContext.HttpContext.Request; 
      return req.Form[this.Name] == this.Value; 
     } 
    } 
+0

的ModelState.IsValid是最有可能的模型綁定引起 - 可能對Property參數!您可以在返回視圖之前調用ModelState.Clear()? –

+0

你做錯了什麼(有兩個不同的動作非常奇怪 - 「添加」和「創建」調用單一動作方法)。你能描述你編程的過程嗎? –

+0

正如我已經解釋了'Property'和'DList'的存在當用戶開始創建一個新的'Property'時,它顯示'Name'的文本框和'DataType'的下拉列表,以及從' DType'選擇'List'值,出現一個文本框,允許將值保存到'DList'模型。這將是'textboxList'和'Add'按鈕。當單擊添加按鈕時,它應該只爲「DList」添加一個值,並允許添加更多值。然後當用戶點擊'Create'時,它應該保存創建的'Property'。希望這是很清楚的。 – rexdefuror

回答

0

db.DLists.Add(dList);但千萬不要把它保存在數據庫中,你可以返回視圖。你可以把db.Configuration.ValidateOnSaveEnabled = false;上述db.DLists.Add(dList);,看看是否可行,但我想應該是這樣的:

db.Configuration.ValidateOnSaveEnabled = false; 
db.DLists.Add(dList); 
db.SaveChanges(); 
return View(property); 

您可能希望在返回前關閉驗證在救回來的。

更新 也許你可以試試這個:

@{ 
    var textBoxData = form.find('input[name="textboxList"]').val(); 
} 
<input type="button" value="Add" title="Add" onclick="location.href='@Url.Action("Create_Add", "Controller", new { textboxList = textBoxData })'" /> 
+0

試過了。仍然檢查相同的...... :( – rexdefuror

+0

當你點擊添加時,你確定表達按鈕==「添加」是真的嗎?你可以在那裏設置斷點並檢查它嗎? –

+0

我檢查了你是對的,它沒有通過那個表達式... 我怎樣才能在不同按鈕上點擊事件? 這是我以前用過的一種方式,但是在一個較不復雜的情況下......而且它的工作原理 我也許可以打電話給另一個從點擊形式? '' – rexdefuror