微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过Html.BeginForm和Ajax.ActionLink

如何解决通过Html.BeginForm和Ajax.ActionLink

我目前正在尝试找出解决以下问题的MVC方式:

我有一个基于模型的表(索引视图),并且第一行有一个按钮来更改此行的一个单元格。按下按钮后,将显示一个弹出窗口(通过Ajax.ActionLink),要求提供附加注释。弹出视图是通过部分视图和Html.BeginForm实现的。添加评论并单击提交按钮后,将显示更新的索引视图。

此刻,在显示更新的索引视图之前,我计算了第一行项目的ID,因为我不知道如何在最初的之间通过Ajax.ActionLink传递此ID。强烈{@ {1}}。

整个过程如下:

索引视图中的Ajax.ActionLink:

Html.BeginForm

更新部分视图:

@Ajax.ActionLink("Update","Update",null,new AjaxOptions { HttpMethod = "GET",UpdateTargetId = "result",InsertionMode = InsertionMode.Replace,OnSuccess = "openPopup()" },new { @class = "btn btn-info"})

控制器中的操作结果

@using (Html.BeginForm("Index"))
{
    <div class="form-group">
        <h4>Title</h4>
        <p>Comment here:</p>
        <textarea type="text" class="form-control" name="comment" id="comment" required></textarea>
    </div>

    <input type="submit" value="Update" class="btn btn-info" />
}

感谢您的帮助!

解决方法

首先在此处选择ViewBag的第一个参数(例如viewbag或某些视图模型)传递ID进行查看

[HttpPost]
        public async Task<ActionResult> Index(string comment)
        {
            
            //Here I calculate the ID,which represents the first row
            //I would like to have this ID as a paramter,additional to comment

            int maxID = dbContext.db.Max(m => m.pmc_id);
            db x= dbContext.db.Where(m => m.pmc_id == maxID).FirstOrDefault();

            //...Update database etc.
            ViewBag.CurrentId = calculatedId;//saved Id to pass to View
            return RedirectToAction("Index");
        } 

让ID添加到视图中

  @using (Html.BeginForm("Index"))
    {
        <input type="hidden" value="@ViewBag.CurrentId" name="Id"/>
        <div class="form-group">
            <h4>Title</h4>
            <p>Comment here:</p>
            <textarea type="text" class="form-control" name="comment" id="comment" required></textarea>
        </div>
    
        <input type="submit" value="Update" class="btn btn-info" />
    }

现在让我们通过mvc解析器获取Id参数

  [HttpGet]
    public PartialViewResult Update(int Id)//here is your id
    {                   
        return PartialView();
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。