简体   繁体   English

ASP.NET C#MVC-在foreach中捕获项目的值并传递给模态

[英]ASP.NET C# MVC - Capturing value of item in foreach & passing to modal

This may end up being a much simpler problem than I'm making it out to be, but here goes... 这可能最终比我提出来的要简单得多,但是这里...

I have a BookManager class and a Book class. 我有一个BookManager类和一个Book类。 BookManager has many Books - this is setup through EntityFramework using CodeFirst. BookManager有很多书-这是通过使用CodeFirst通过EntityFramework进行设置的。

In the Details page of BookManager, I'm using a foreach statement to display a list of Books along with a link to Edit the specific book. 在BookManager的“ 详细信息”页面中,我正在使用foreach语句来显示“ 书籍”列表以及“ 编辑特定书籍”的链接。

Here's that code: 这是代码:

@foreach (var item in Model.Books)
    {

        <tr>
           <td>
              @Html.DisplayFor(modelItem => item.BookName)
            </td>
            <td>
              @Html.DisplayFor(modelItem => item.BookNumber)
            </td>
            <td>                                   
              <a class="btn" data-toggle="modal" href="#BookEditModal" @{ViewBag.SelectedBook = item.BookID}>
              <i class="icon-edit"></i>
                 Edit       
              </a>                                

             </td>
         </tr>
      }

Here is the tricky part: 这是棘手的部分:

<a class="btn" data-toggle="modal" href="#BookEditModal" @{ViewBag.SelectedBook = item.BookID} >
    <i class="icon-edit"></i>
       Edit       
</a>  

Normally I would do something like this: href="@Url.Action("Edit", "Book", new { id = item.BookID}) 通常,我会这样做: href="@Url.Action("Edit", "Book", new { id = item.BookID})

However, I am using the href attribute to open a modal window which will use a Html.RenderAction to display the appropriate form coming from the Book controller. 但是,我使用href属性打开一个模式窗口,该窗口将使用Html.RenderAction来显示来自Book控制器的适当表单。

Here is the Modal window that is contained at the bottom of the BookManager Details page: 这是“书本管理器详细信息”页面底部包含的“模态”窗口:

 <div class="modal hide fade in" id="BookEditModal" )>
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h3>Edit Book</h3>
          </div>
          @using (Html.BeginForm("Edit", "Book", FormMethod.Post))
          {
             <div class="modal-body">
               @{ Html.RenderAction("Edit", "Book", new { id = ViewBag.SelectedBook}); }
             </div>

          }
        </div>

Since I need to pass the id into the RenderAction in order for the Edit method to work, I need the value of the item that was clicked in the foreach - but that is no longer in scope. 由于我需要将id传递给RenderAction才能使Edit方法起作用,因此我需要在foreach中单击的项目的值-但这已不在范围内。

I tried creating a ViewBag.SelectedBook within the Details method of the BookManager, but instead of the value being the selected book, instead the foreach statement loops through and the value of ViewBag.SelectedBook ends up being the Count of Books. 我尝试在BookManager的Details方法中创建ViewBag.SelectedBook,但不是将值作为所选书,而是使foreach语句循环通过,并且ViewBag.SelectedBook的值最终成为书数。 (eg. If I have 3 Books in the list, than ViewBag.SelectedBook equals 3). (例如,如果我在列表中有3本书,则ViewBag.SelectedBook等于3)。

I thought about using JQuery and calling $(this) on the clicked item, but then I would have to collect the BookID with Javascript and convert that back to C# in order to collect it in the Html.RenderAction - that just seems way overboard for what I'm trying to do. 我曾考虑过使用JQuery并在单击的项目上调用$(this) ,但随后我将不得不使用Javascript收集BookID并将其转换回C#,以便将其收集到Html.RenderAction中-似乎对于我正在尝试做的。

How would I go about collecting the value of the item clicked and passing that along to the Html.RenderAction that is in the modal? 我将如何收集被单击的值并将其传递给模式中的Html.RenderAction

Thanks! 谢谢!

There is no other way. 没有别的办法了。

What I mean by that is.. you're attempting to have server-side code interact with client side scripts. 我的意思是..您正在尝试使服务器端代码与客户端脚本进行交互。

Since your modal dialog is rendered at the time the page is rendered, what you're attempting won't work. 由于您的模态对话框是在页面呈现时呈现的,因此您尝试执行的操作将不起作用。

My suggestion, is to load the entire modal dialog, via a controller action, perhaps like this: 我的建议是通过控制器动作加载整个模式对话框,也许像这样:

Generate your foreach links, like this: 生成您的foreach链接,如下所示:

<a class="btn edit-book-link" data-toggle="modal" href="/ControllerName/BookForm/@item.BookID">

..make your modal dialog div blank, like this: ..将模态对话框div设为空白,如下所示:

<div class="modal hide fade in" id="BookEditModal" )>
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">×</button>
        <h3>Edit Book</h3>
    </div>
    <div id="modalContent"></div>
</div>

Create a view that takes an integer as it's model, called, for example, BookForm: 创建一个采用整数作为模型的视图,例如,称为BookForm:

[HttpGet]
public ViewResult BookForm(int id) {
    return View(id);
}

..then, use jQuery to load the correct form on every click: ..然后,使用jQuery在每次点击时加载正确的表单:

$(function () {
    $('.edit-book-link').click(function (e) {
        $('#modalContent').load($(this).attr('href'), function() {
            // show your modal dialog here using whatever method you use..
        });
        e.preventDefault();
        return false;
    });
});

Then in your BookForm view, you can generate your form using the ID. 然后,在BookForm视图中,可以使用ID生成表单。

Hopefully I understood your issue correctly. 希望我能正确理解您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM