【发布时间】:2015-06-19 06:58:28
【问题描述】:
我有 3 个视图(1 个索引、2 个联系人(部分视图)、3 个详细信息(部分视图))
我有一个数据库,其中包含 2 个由 ContactId 绑定的表,我可以使用这些表从数据库中获取要显示的详细信息。我使用 ADO 制作数据库模型。这 2 个表(类)被命名为 Contact 和 ContactTelefon。
我尝试使用@html.ActionLink(如您在联系人视图中看到的)而不是按钮来从行中获取 ID,但这会将我带到一个新页面,它甚至不显示详细信息。
我的问题是:我怎样才能让详细信息显示在文本框中,以便我可以编辑数据。
就用户而言,所有操作都必须在同一视图中。
控制器:
ContactsDbEntities db = new ContactsDbEntities();
[HttpGet] //Index
public ActionResult Index()
{
return View();
}
//Contacts
public ViewResult Contacts()
{
var contactsList = db.Contacts.ToList();
return View(contactsList);
}
//Details
public ActionResult Details(int? id)
{
ContactTelefon contactTel = db.ContactTelefons.Find(id);
return View(contactTel);
}
索引视图
@using Demo.Models
@model Contact
@section scripts
{
<link href="~/Content/jquery-ui.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-ui.min.js"></script>
<script src="~/Scripts/jquery-ui.js"></script>
<script>
$(function () {
$(document).on('click', '#Details', function () {
$.get('@Url.Action("Details","Home")', function (data) {
$('#divDetails').replaceWith(data);
});
});
</script>
}
<table id="mainTable" class="table table-bordered table-striped">
<tr>
<th>
@Html.DisplayNameFor(model => model.ContactId)
</th>
<th>
@Html.DisplayNameFor(model => model.Nume)
</th>
<th>
@Html.DisplayNameFor(model => model.Prenume)
</th>
<th>
@Html.DisplayNameFor(model => model.Adresa)
</th>
<th>
@Html.DisplayNameFor(model => model.Mentiuni)
</th>
</tr>
<tr>
<th>
</th>
@using (Html.BeginForm())
{
<th>
@Html.TextBoxFor(model => model.Nume, null, new { id = "txtSearchNume", @class = "form-control" })
</th>
<th>
@Html.TextBoxFor(model => model.Prenume, null, new { id = "txtSearchPrenume", @class = "form-control" })
</th>
<th>
@Html.TextBoxFor(model => model.Adresa, null, new { id = "txtSearchAdresa", @class = "form-control" })
</th>
<th>
@Html.TextBoxFor(model => model.Mentiuni, null, new { id = "txtSearchMentiuni", @class = "form-control" })
</th>
<th>
<input type="submit" value="Create" class="btn btn-success"
onclick=" location.href='@Url.Action("Index", "Home")' " />
</th>
<th>
<input type="submit" name="submitSearch" value="Search" class="btn btn-info"
onclick=" location.href='@Url.Action("Create", "Home")' " />
</th>
<tr>
@{Html.RenderAction("Contacts", "Home");}
</tr>
<tr><div id="divDetails"></div></tr>
}
</table>
联系人视图
@using Demo.Models
@model IEnumerable<Contact>
<table class="table table-bordered table-hover">
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ContactId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Nume)
</td>
<td>
@Html.DisplayFor(modelItem => item.Prenume)
</td>
<td>
@Html.DisplayFor(modelItem => item.Adresa)
</td>
<td>
@Html.DisplayFor(modelItem => item.Mentiuni)
</td>
<td>
@Html.ActionLink("Delete", "Delete", new { id = item.ContactId },
new { @class = "btn btn-danger", onclick = "return confirm('Delete this record?');" })
</td>
<td>
<input id="Details" type="button" name="Details"
value="Details" class="btn btn-info" />
</td>
<td>
@Html.ActionLink("DetailsLink","Details",new{id = item.ContactId})
</td>
</tr>
}
</table>
详情查看
@using Demo.Models
@model ContactTelefon
<div class="form-horizontal">
<div claass="form-group">
@* must get the id from Contacts *@
@Html.LabelFor(model => model.ContactId)
@Html.LabelFor(model => model.ContactTelefonId)
@Html.LabelFor(model => model.NumarTelefon)
@Html.LabelFor(model => model.TipNumarTelefon)
</div>
<br />
<div claass="form-group">
@Html.DisplayFor(model => model.ContactId)
@Html.DisplayFor(model => model.ContactTelefonId)
@Html.DisplayFor(model => model.NumarTelefon)
@Html.DisplayFor(model => model.TipNumarTelefon)
</div>
<div claass="form-group">
@Html.EditorFor(model => model.ContactId)
@Html.EditorFor(model => model.ContactTelefonId)
@Html.EditorFor(model => model.NumarTelefon)
@Html.EditorFor(model => model.TipNumarTelefon)
</div>
</div>
【问题讨论】:
-
如果联系人和详细信息是部分视图,那么您不应该有一个 actionMethod 调用他们的模型。而是使用检索模型的索引操作方法创建视图模型,然后将模型从视图模型传递到局部视图。这并不能回答问题,但应该是朝着正确方向迈出的一步。
-
stackoverflow.com/questions/5142422/… 这是一个用于获取所选行 ID 的链接。在你的 ajax 调用中传递它并相应地构建你的模型
-
@Matt Bodidly 这有点意思,但这有点硬编码。我需要数据库中的 ContactId。我尝试使用 ActionLink,就像 Delete Action 正在使用它一样,但我没有得到任何结果,它甚至没有像我想要的那样显示在同一页面上。
-
所以当您单击链接时,您希望详细信息内嵌在列表页面上吗?
-
我需要详细信息显示在可以是简单表格行的详细信息部分视图中,也需要显示在文本框中,以便用户可以编辑它们。
标签: c# asp.net-mvc asp.net-mvc-4