因此,我有這個Property
類,它由ID
Name
和DataType
定義。 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;
}
}
的ModelState.IsValid是最有可能的模型綁定引起 - 可能對Property參數!您可以在返回視圖之前調用ModelState.Clear()? –
你做錯了什麼(有兩個不同的動作非常奇怪 - 「添加」和「創建」調用單一動作方法)。你能描述你編程的過程嗎? –
正如我已經解釋了'Property'和'DList'的存在當用戶開始創建一個新的'Property'時,它顯示'Name'的文本框和'DataType'的下拉列表,以及從' DType'選擇'List'值,出現一個文本框,允許將值保存到'DList'模型。這將是'textboxList'和'Add'按鈕。當單擊添加按鈕時,它應該只爲「DList」添加一個值,並允許添加更多值。然後當用戶點擊'Create'時,它應該保存創建的'Property'。希望這是很清楚的。 – rexdefuror