【发布时间】:2016-01-26 11:37:42
【问题描述】:
我有一个普通的索引页面,其中显示了数据库中的内容列表。我的索引中有一个创建按钮,该按钮现在重定向到另一个页面以创建一个新项目。
首先我尝试了部分视图以使其正常工作,但页面仍然重定向到另一个没有布局的页面。然后我尝试使用 Jquery 隐藏/显示包含 Create 的 HTML 代码的 div。但我无法将值发布到正确的 Action 方法。
到目前为止,我还没有尝试过所有的东西。我想我的索引视图不会引起很大的兴趣。
使用 Jquery 时的索引视图
<div> Displaying the index View</div>
<div id="Create" style="display:none">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.LsystemFamily.FamilyName, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.LsystemFamily.FamilyName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LsystemFamily.FamilyName, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.LsystemFamily.DescriptionEN, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.LsystemFamily.DescriptionEN, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LsystemFamily.DescriptionEN, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.LsystemFamily.DescriptionDE, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.LsystemFamily.DescriptionDE, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LsystemFamily.DescriptionDE, "", new { @class = "text-danger" })
<input type="submit" value="Create" class="btn btn-default" />
}
jQuery
<script type="text/javascript">
$(document).ready(function () {
$('#btn').click(function () {
$('#Create').toggle();
});
});
</script>
控制器(返回局部视图时)
public ActionResult Create()
{
return View("_Create");
}
返回部分视图时的索引视图
<div>
Index View
@Html.ActionLink("Create","Create","Controller");
</div>
@Html.Partial("_Create")
在任何情况下,我的Create 都没有显示我想要的方式。
根据答案,我尝试添加部分视图,然后切换 div。但我收到以下错误
“System.InvalidOperationException”类型的异常发生在 System.Web.Mvc.dll 但未在用户代码中处理
附加信息:传入字典的模型项是 类型 'System.Data.Entity.Infrastructure.DbQuery`1[TEDALS_Ver01.Models.LsystemFamily]', 但是这本字典需要一个类型的模型项 'TEDALS_Ver01.Models.LsystemFamily'。
更新:索引视图
@using (Html.BeginForm())
{
<p>
Search @Html.TextBox("SearchString")
<input type="submit" class="btn btn-default" value="Search" />
</p>
}
<script type="text/javascript">
$(document).ready(function () {
$('#btn').click(function () {
$('#Create').toggle();
});
});
</script>
<table class="table">
<tr>
<th>
Family Name
</th>
<th>
System Count
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td data-toggle="tooltip" title="@item.DescriptionDE" data-placement="right">
@Html.ActionLink(item.FamilyName, "ViewSystems", "LsystemFamilies", new { id = @item.LsystemFamilyID},null)
</td>
<td data-toggle="tooltip" title="Number of Systems in @item.FamilyName" data-placement="right">
@Html.DisplayFor(modelItem => item.LsystemCount)
</td>
<td>
@*@Html.ActionLink("Add System", "Create", "Lsystems", new { id = item.LsystemFamilyID }, null)|*@
@Html.ActionLink("Edit", "Edit", new { id = item.LsystemFamilyID }) |
@Html.ActionLink("Details", "Details", new { id = item.LsystemFamilyID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.LsystemFamilyID })|
</td>
</tr>
}
</table>
<input type="button" id="btn" class="btn btn-default" value="Create">
</div>
<div id="Create" style="display:none">
@Html.Partial("_Create")
</div>
@section Scripts{
<script type="text/javascript">
$(document).ready(function () {
$('#btn').click(function () {
$('#Create').toggle();
});
});
</script>
@Scripts.Render("~/bundles/jqueryval")
}
更新:创建 Post 方法
public ActionResult Create([Bind(Include = "LsystemFamilyID,FamilyName,LsystemCount,DescriptionEN,DescriptionDE,CreatedOn,ModifiedOn,CreatedBy,ModifiedBy")] LsystemFamily lsystemFamily)
{
lsystemFamily.CreatedOn = DateTime.Now;
lsystemFamily.CreatedBy = User.Identity.Name;
lsystemFamily.ModifiedOn = DateTime.Now;
lsystemFamily.ModifiedBy = User.Identity.Name;
lsystemFamily.LsystemCount = 0;
if (ModelState.IsValid)
{
if (db.LsystemFamily.Any(x => x.FamilyName.Equals(lsystemFamily.FamilyName)))
{
ModelState.AddModelError("FamilyName", "Family Name already Exists");
return PartialView("_Create",lsystemFamily);
}
db.LsystemFamily.Add(lsystemFamily);
db.SaveChanges();
return RedirectToAction("Index");
}
return PartialView("_Create",lsystemFamily);
}
【问题讨论】:
-
Index视图中的模型是IEnumerable<LsystemFamily>,但Create视图中的模型是LsystemFamily。您需要将正确的模型传递给部分 - 例如@Html.Partial("_Create", new LsystemFamily()) -
我无法在
Html.Partial中添加参数new LsystemFamily作为参数 -
它的
new LsystemFamily()- 这是overload -
是的。我无法添加
new LsystemFamily。我的智能感知没有显示 LsystemFamily。我可以添加Model.First()来让它工作。但我将在所有文本字段中填充模型的值 -
使用完全限定名称 -
new TEDALS_Ver01.Models.LsystemFamily()或在视图中添加using TEDALS_Ver01.Models;
标签: javascript c# jquery html asp.net-mvc