1

我想将选定的单元格值传递@Html.DisplayFor(model => item.year)Url.Action我在 javascript 中创建的单元格值。

我的控制器有两个参数,一个是静态名称,另一个是从所选行动态获取的年份。

表代码:

<table id="name" border=1 width="50%">
    <tr>
        <th>Name</th>
        <th>Year</th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                <button id="see">@Html.DisplayFor(model => item.year)</button>
            </td>
        </tr>
    }
</table>

javascript代码:

<script type="text/javascript">
    var urls = {
       view: '@Html.Raw(@Url.Action("(actionName)", "(controller)", new { name = "Mary", year = (this is what idk)}))'
    };

    $document.ready(function()
    {
        $('#see').click(function () {
            $('#dialog').load(urls.view, function () {    
            });
        });
    });
</script>

控制器方法:

public ActionResult actionName(string name, string year)
{
    var query = new aquery();
    var response = query.Fetch(name, year);
    return View(response);
}

调用具有

<form id="", action="@html.raw(@url.action(same thing here as my previous main page. what do i put for the year here now?))">
4

2 回答 2

0

You dont need to add @Html.Raw before @Url.Action, because, as you can see in the Documentation, @Url.Action returns a String

var urls = { view: '@Url.Action("actionName", 
                                "controllerName", 
                                 new {name = "mary", year = ?})' }
于 2015-11-16T02:57:23.003 回答
0

id="see"由于您添加到foreach循环中的按钮的重复属性,您的 html 无效。首先将其更改为使用类名,并添加,type="button"因为默认为"submit"

<button type="button" class="see">@Html.DisplayFor(model => item.year)</button>

脚本应该是

<script type="text/javascript">
  $document.ready(function()
  {
    var dialog = $('#dialog'); // cache elements you may repeatedly use
    var url = '@Url.Action("actionName")'; // add the controller name if its in a different controller
    $('.see').click(function () { // change selector to use the class name
      dialog.load(url, { name: 'Mary', year: $(this).text() });
    });
  }
</script>

旁注:您的方法参数应该是int year, notstring year并且您的方法应该返回 a PartialView(response), notView(response)

于 2015-11-17T02:08:28.780 回答