如何将按钮值从模式窗口(使用局部视图)传递到父视图

时间:2018-11-06 14:12:26

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

这是局部视图上的按钮,

<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值从部分视图传递到创建视图,然后将其作为隐藏值传递给创建控制器

1 个答案:

答案 0 :(得分:0)

由于要在页面加载后尝试更改输入字段的值,因此这需要在用户的浏览器中进行,因此在服务器端运行的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>