【发布时间】:2014-07-14 21:58:07
【问题描述】:
我创建了以下简单的 MVC 5 应用程序
- 创建具有 4 个属性的模型(首先使用代码)
- 我去控制器,通过脚手架生成视图。
这工作正常! 当我运行页面并单击创建按钮 我已导航到新页面 时,我需要创建的字段(这是默认行为), 我的问题是,是否有一种方法可以让我在单击创建时停留在同一页面上并在表格的开头添加新的空行,即内联创建只有一页 创建只是在表格中打开带有文本框和复选框的新行,并在右侧有保存按钮。
这是我的简单模型和脚手架生成的 UI
namespace Emp01.Models
{
public class Emp
{
public string name { get; set; }
public Boolean checkBox1 { get; set; }
public Boolean checkBox2 { get; set; }
public Boolean checkBox3 { get; set; }
}
public class EmpDbContext : DbContext
{
public EmpDbContext()
: base("DefaultConnection")
{
}
public DbSet<Emp> Emps { get; set; }
}
}
这是风景
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
<th>
@Html.DisplayNameFor(model => model.checkBox1)
</th>
<th>
@Html.DisplayNameFor(model => model.checkBox2)
</th>
<th>
@Html.DisplayNameFor(model => model.checkBox3)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.checkBox1)
</td>
<td>
@Html.DisplayFor(modelItem => item.checkBox2)
</td>
<td>
@Html.DisplayFor(modelItem => item.checkBox3)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}
</table>
更新
另外喜欢 Patil 建议我尝试以下方法,我需要你的帮助
- 我将在创建操作中生成的代码添加到索引页面,并将类形式从垂直更改为内联。
2.添加新的 div 与应该切换的上下文 ID。
目前我错过了两件事
- 当我点击按钮时,我应该如何让工具工作
2.当我运行页面时,链接代码中出现错误(model=>model.name,因为它的当前为空,但当您调用创建页面时,它也是空的,所以我不明白为什么......)
我需要你帮忙!
<script>
jQuery(document).ready(function () {
jQuery(".content").hide();
});
$("#addmoret").click(function () {
$(".content").toggle();
});
</script>
@* ----------------------------------------------------------- *@
<div class="content">
<div class="form-inline">
<h4>Role</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.checkBox1, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@*@Html.EditorFor(model => model.checkBox1)*@
@Html.ValidationMessageFor(model => model.checkBox1, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.checkBox2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@*@Html.EditorFor(model => model.checkBox2)*@
@Html.ValidationMessageFor(model => model.checkBox2, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.checkBox3, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@*@Html.EditorFor(model => model.checkBox3)*@
@Html.ValidationMessageFor(model => model.checkBox3, "", new { @class = "text-danger" })
</div>
</div>
</div>
更新 2
@Html.LabelFor(model => model.name, htmlAttributes: new { @class= "control-label col-md-2" })
1:“System.Collections.Generic.IEnumerable”不包含“name”的定义,并且没有扩展方法“name”接受“System.Collections.Generic.IEnumerable emp01.Models.emp”类型的第一个参数> ' 可以找到(您是否缺少 using 指令或程序集引用?)
对于我添加的按钮
<button type='button' id='addmore' class="btn ui-state-default medium" value='Add More Credit ?' style="width: 200px;">Create </button>
【问题讨论】:
-
$("#addmoret").click(function (){$(".content").toggle();});在这里,“#addmoret”是按钮的 id,所以请确保它是按钮的 id。顺便问一下,您是否遇到任何错误或异常?
-
@KrunalPatil-请查看我的更新...
-
如果您可以看到这是一个错字:您的按钮 id 是“addmore”并且您使用的是“addmoret”,这可能会给您带来错误。请更正。
-
@KrunalPatil- 谢谢我已经修复了,但是模型错误仍然存在,请您帮忙
-
如果你告诉我你传递给你的页面 html 页面、模型还是列表?像这样的东西(@model SampleAsyncPartialViews.ViewModels.HomeViewModel)
标签: jquery css asp.net .net asp.net-mvc