【问题标题】:MVC get details from partial view to show in textboxesMVC 从局部视图中获取详细信息以显示在文本框中
【发布时间】: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


【解决方案1】:

似乎您正在从 ASP.NET WebForms 开始 MVC。 MVC 的特点是它不像 WebForms 那样具有魔力,因此您必须对幕后发生的事情有一个很好的了解才能进行平滑的过渡。另外,从外观上看,您的数据库模型使用实体框架。

首先,您处理Details 按钮的方式完全错误。你应该做的是:

HTML

<input type="button" name="Details" value="Details" class="btn btn-info js-details" 
    data-id="@item.ContactId" />

JavaScript

$(document).on('click', '.js-details', function (event) {

    // get the element that triggered the event
    var $element = $(event.currentTarget);
    var id = $element.data('id');

    // you might have to type in the literal URL if you have a custom route 
    // here
    $.get('@Url.Action("Details","Home")'+ '?id=' + id, function (data) {
        $('#divDetails').html(data);
    });
});

让我知道这是否适合您。您还有其他可以改进的地方,但这应该是一个不错的开始。

【讨论】:

  • 每天我都想知道为什么我在不了解主要概念的情况下继续尝试使用 MVC,但它让我继续前进。不知道具体是什么。我会谈论几个小时......无论如何,你的解决方案是我开始所需要的。我也会调整它以适应其他一些需求。谢谢!
  • 如果您对 MVC 中的各种概念有任何疑问,我非常乐意提供帮助。我已经做了一段时间了,我知道大部分的来龙去脉以及最佳实践。我不是 Jon Skeet,但我可以提供帮助。
  • 我不知道从哪里开始!实际上,我无法掌握将数据从控制器发送到视图的无数方法。尤其是在处理“强类型视图”时。你从哪里开始使用 MVC?你做了什么让自己感到舒服?每天我都觉得我知道了很多概念,每天我都在学习新的知识,但我觉得我什么都不知道,这可能是 99% 的。
  • 我在 MVC 之前做过 PHP 和 ASP.NET WebForms,当我开始的时候,它就像没有别的东西一样。我花了好几年的时间才能很好地掌握它。我能给你的最好建议与让我进入 MVC 的朋友给我的建议相同:MVC 没有魔法。事实上,它的工作方式(如路由和控制器/视图解析)有一些“魔力”,但这些都在幕后深处。对于像您这样的新手来说,这是一个很好的建议,因为您必须了解 MVC 不会为您做任何事情。如果某件事不起作用,那是因为您错过了一部分。
猜你喜欢
  • 2022-10-17
  • 1970-01-01
  • 2014-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多