【问题标题】:Razor @Html.ActionLink not getting passed control value correctlyRazor @Html.ActionLink 没有正确传递控制值
【发布时间】:2018-11-27 16:18:33
【问题描述】:

我是 Razor 的新手。我在这个项目上取得了良好的进展,但遇到了一个似乎很容易的主要障碍。我已经阅读了很多关于如何将控件的值作为参数传递给控制器​​以重定向到新视图的帖子。问题是我要么将值传递给控制器​​但无法重定向,要么我重定向并且参数未传递。

这是我最近的尝试。我希望将 GetSelectedEmail 的返回值传递给控制器​​(“selectedEmail”的值)。我可以看到 Javascript 正在获取正确的值并且正在调用控制器,但该值始终为 NULL。

@Html.ActionLink("Get Scoring Report...", "History", "Student", null, new { onclick = "return GetSelectedEmail();" });
<select id="selectedEmail" name="align">
    @foreach( var s in Model.Students )
    {
        <option id=@s.Email>@s.Email</option>
    }
</select>

function GetSelectedEmail() {
    var str = "new {email=" + $("#selectedEmail").val() + "}";
    return str;
}

控制器...

    public ActionResult History(string email, string sort)
    {
        string localEmail="";

        if ( email == null || email == "" )
            localEmail = AccountProfile.CurrentUser.UserName;

...

我还尝试使用 Ajax 调用控制器,如下所示。控制器确实获得了“selectedEmail”参数,但页面从不重定向。我只是什么都不做。我尝试了是否有带有链接参数的操作链接(如下所示)。

@Html.ActionLink("Get Scoring Report...", "", "", null, new { onclick = "return GetSelectedEmail();" });
<select id="selectedEmail" name="align">
    @foreach( var s in Model.Students )
    {
        <option id=@s.Email>@s.Email</option>
    }
</select>


function GetSelectedEmail() {
    $.ajax({
        url: '/Student/History',
        data: { email: $("#selectedEmail").val(), sort: "CaseID" },
        type: 'POST',
        dataType: 'json',
    });
    return true;
}

有什么想法吗?

【问题讨论】:

    标签: asp.net razor html.actionlink


    【解决方案1】:

    对于我需要的解决方案很简单。

    <a href="#" onclick="GetScoreHistory()">Show Student Scores</a>
    <select id="selectedEmail" name="align">
        @foreach( var s in Model.Students )
        {
            <option id="@s.Email">@s.LastName,@s.FirstName</option>
        }
    </select>
    

    还有 Javascript 的魔力……

    function GetScoreHistory() {
        var emailVal = $('#selectedEmail').find('option:selected').attr('id');
        var url = '@Url.Action("History", "Student")';
        url += "?email=" + escape(emailVal);
        window.location.href = url;
    }
    

    控制器的调用方式完全符合我的需要。

    【讨论】:

      【解决方案2】:

      您的第一种方法实际上并没有进行重定向。 (它也调用了不同的方法,我假设是复制粘贴错误)

      您当前的代码没有传递值,因为它是一个链接,当它被点击时,它应该导航到那个 url,这正是它正在做的事情。

      我刚刚更改了代码以使用不显眼的 javascript。将链接的onclick 替换为id

      @Html.ActionLink("Get Scoring Report", "History", "Student", null, new { id="score" });
      

      当点击此链接时,读取 select 元素的值并通过设置 location.href 属性值导航到第二个操作方法

      $(function () {
      
          $("#score").click(function(e) {
              e.preventDefault();  // Stop the normal redirection
      
              var url = $(this).attr("href"); //Get the url to action method
      
              url += "?email=" + $("#selectedEmail").val(); //append querystrings
      
              window.location.href = url;  // redirect to that url
          });
      
      });
      

      【讨论】:

      • 很好,感谢您的回复。我看到您将 ActionLink 参数的“id”设置为“score”,然后我看到了 score 对象的 click 方法如何完成重定向。你能解释一下这个结构是如何/为什么定义的,或者可以指出一些文档吗?看起来 "new { id="score"} 是 htmlAttributes 参数?
      • 该代码将生成一个带有id='score' 的锚标记。稍后在 javascript 中,我们可以使用这个 Id 作为 jQuery 选择器来连接点击事件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2014-02-19
      • 1970-01-01
      • 2021-01-23
      相关资源
      最近更新 更多