因此,我有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;
}
}
答案 0 :(得分: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 })'" />