MVC弹出子页面向父页面传值

本文介绍了一个使用JavaScript在父页面与子页面之间传递数据的示例。通过`showModalDialog`方法打开子窗口,并定义`sendvalue`函数将选中的值返回给父页面。

实现思路是使用js在父子页面间传值

视图一代码,父页面

@{
    ViewBag.Title = "Index";
}

<script type="text/javascript">
    //alert("1");
    function test()
    {
        window.showModalDialog('test/listtest', 'newwindow', 'height=400,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no, status=no')
        var temp = window.showModalDialog('test/listtest', 'newwindow', 'height=400,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no, status=no')
        document.getElementById("test").value = temp;
    }
</script>

<h2>@ViewData["Message"]</h2>
<p><input type="submit" value="submit" onclick="test()"></p>
<p><input type="text" id="test"></p>

视图二代码:子页面

@model IEnumerable<mvctest.Models.student>

@{
    ViewBag.Title = "listtest";
}

<script type="text/javascript">
    function sendvalue(a) {
        alert(a);
        document.getElementById("test").value = a;
        window.returnValue = a;
        window.close();
    }
</script>
<p>
    <input type="submit" value="sendvalue" onclick="sendvalue()">
    <input type="text" id="test" value=sendvalue>
</p>

<h2>listtest</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table id="table1">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.password)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.descript)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.password)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.descript)
        </td>
        <td>
            <input type="button" value="haha" onclick="sendvalue('@item.name')">
        </td>
    </tr>
    
}

</table>

 

转载于:https://www.cnblogs.com/lovejunjuan/p/4689212.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值