2012-08-29 119 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