【问题标题】:How to pass button value from a modal window (that uses partial view) to the parent view如何将按钮值从模式窗口(使用局部视图)传递到父视图
【发布时间】:2018-11-06 14:12:25
【问题描述】:

这里是局部视图上的按钮,

<button type="button" name="btnid" id="bparentid" value="@item.Itemid " class="btn btn-success" onclick="alert(@item.Itemid)">Select</button>

只是为了检查它是否包含我放置剃须刀警报功能的值,并且我希望使用父视图中的隐藏输入表单在按钮单击时将按钮值传递给父视图

<input asp-for="Itemid" type="hidden" value=" @item.Itemid" />

更多细节:

我有两张表分别记录孩子和他们的父母, 这是模型

public class Children
{
   [Key]
    public int ChildID { get; set; }
    [Required] [StringLength(50)]
    public string FirstName { get; set; }
    [ForeignKey("Parentid")]
    public virtual Parent Parent { get; set; }
  }

public class Parent
{
    [Key]
    public int Parentid { get; set; }
    public string FullName { get; set; }
    public virtual IEnumerable<Children> Children { get; set; }
}

这里是用于发布新子记录及其父信息的 Create 方法控制器:

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("ChildID,FirstName")] Children children, int existingParentid,
        [Bind("Parentid,FullName")] Parent parent, bool existingparent)
    {
        if (ModelState.IsValid)
        {
            if (existingparent)
            {
                children.Parentid = existingParentid;
                _context.Add(children);
                await _context.SaveChangesAsync();
                return View(children);
            }else
            _context.Add(children);
            await _context.SaveChangesAsync();
            _context.Add(parent);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(children);
    }

这里是模态局部视图的控制器方法

public async Task<IActionResult> ModalAction()
    {
        var MyDbContext = _context.Children.Include(C => C.Parent);
        return PartialView(await MyDbContext.ToListAsync());
    }

这里是 Create 方法的视图

<div class="row">
<div class="col-md-4">
    <form asp-action="Create">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="FirstName" class="control-label"></label>
            <input asp-for="FirstName" class="form-control" />
            <span asp-validation-for="FirstName" class="text-danger"></span>
        </div>
        @Html.ActionLink("Select Existing Parent", "ModalAction", "Children", null, new { @class = "btn btn-flat margin",
        data_target = "#searchparent", data_toggle = "modal" })
            <input asp-for="Parentid" type="hidden" value=" " />   
        <div class="modal fade" id="searchparent">
            <div class="modal-dialog">
                <div class="modal-content">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="Parent.FullName" class="control-label"></label>
            <input asp-for="Parent.FullName" class="form-control" />
            <span asp-validation-for="Parent.FullName" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </form>
</div>

这里是模态局部视图

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
     <h4 class="modal-title">Search for Parent data table </h4>
</div>
<div class="modal-body">
    <table class="table">
        <thead>
            <tr>
                <th>  
                    @Html.DisplayNameFor(model => model.FirstName)
                </th>  
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.FirstName)
                    </td>
                    <td>
                        <button type="button" name="bparentid" id="bparentid" value="@item.ChildID" class="btn btn-block btn-success btn-xs"
                                onclick="alert(@item.ChildID)">
                            Select
                        </button>
                    </td>
                </tr>
            }
        </tbody>
    </table>
</div>

问题:

如何将@item.ChildID 值从局部视图传递到创建视图,然后将其作为隐藏值传递给创建控制器

【问题讨论】:

  • 我们需要更多信息。请提供mcve
  • 对不起,我试图尽可能少地提供有关问题域的清晰度。 – 我将用更多代码编辑问题……如果你建议我应该包括什么会很好
  • @Win 希望现在信息足以调查问题

标签: razor asp.net-core asp.net-core-mvc


【解决方案1】:

由于您试图在页面加载后更改输入字段的值,这需要在用户的浏览器内部进行,因此运行服务器端的 Razor 无法提供帮助。但是你可以使用 javascript 完成你想要的:

//First add an ID to your hidden input 
<input id="my-input" ... />

//Second add the following onclick to your button:
<button onclick="document.getElementById('my-input').value = this.value" ...></button>

【讨论】:

  • 谢谢你的回答,这就是我所做的,问题是没有在部分视图中访问项目ID,正如你所说,我可以通过将项目ID分配为按钮来获取项目ID像这样的值`value="@item.Itemid"。但随后我想将按钮值传递给父窗口(视图),然后将其作为隐藏字段发布到控制器
  • 啊,我想我现在明白了 - 谢谢,我已经更新了我的答案。
  • 我在问题中添加了更多细节
猜你喜欢
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
  • 2012-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多