我有一个普通的索引页面,其中显示了数据库中的内容列表。我的索引中有一个创建按钮,现在重定向到另一个页面以创建一个新项目。
首先,我尝试使用部分视图来使其工作,但页面仍然重定向到没有布局的另一个页面。然后我尝试使用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。但我得到以下错误
类型&#39; System.InvalidOperationException&#39;的例外情况发生在 System.Web.Mvc.dll但未在用户代码中处理
附加信息:传递到字典中的模型项是 类型 &#39; System.Data.Entity.Infrastructure.DbQuery`1 [TEDALS_Ver01.Models.LsystemFamily]&#39 ;, 但是这个字典需要一个类型的模型项 &#39; TEDALS_Ver01.Models.LsystemFamily&#39;
更新:索引视图
@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);
}
答案 0 :(得分:1)
你想要做什么,我的意思是如果你想在按钮上点击创建,然后只需创建一个带有Layout = null的部分视图,即“_Create”(不需要动作),现在只需渲染这个您正在切换的div中的局部视图:
Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
this.context=context;
}
如果要刷新索引页面,则在提交create request时,您可以在_create视图中使用Ajax.BeginForm,也可以通过jquery ajax发布它。
答案 1 :(得分:0)
<script type="text/javascript">
$(document).ready(function () {
$('#btn').click(function () {
$('#Create').toggle();
});
});
</script>
您正在尝试按ID访问#btn
,但是您将其设置为类。btn
是一个类名。所以JQuery无法找到您的按钮。就像这样
<script type="text/javascript">
$(document).ready(function () {
$('.btn').click(function () {
$('#Create').toggle();
});
});
</script>