【问题标题】:how to pass string parameter from a view to another view如何将字符串参数从视图传递到另一个视图
【发布时间】:2014-05-08 13:31:05
【问题描述】:

我的索引视图

@model Maintenance_.Models.IndexModel
@{
    ViewBag.Title = "Technician";    
}
...Header...
...Ajax Codes...
<div style="border:1px groove #E0EBEB; width:400px;height:330px;float:left;margin-left:20px;"class="scroll-pane">
    <table border="1px"style="float:left;">
        <thead>
            <tr>
                <td style ="background-color:#7BB7FA; height:40px;width:150px; border-bottom:5px solid; text-align:center;"><b>Technician No.</b></td>
                <td style ="background-color:#7BB7FA; height:40px;width:250px; border-bottom:5px solid; text-align:center;"><b>Technician Name</b></td>
            </tr>
        </thead>
        <tbody>
            @{
                var altRow = false;
                foreach (var item in Model.fNameList)
                {
                    <tr>
                        <td style ="background-color:@(altRow ? "#fff" : "#E0EBEB"); height:40px;width:100px; padding-left:20px;"><a href ="#" onclick='call("@item.techNo");' style ="text-decoration:none; text-decoration-color:black;"> @item.techNo</a></td>
                        <td style ="background-color:@(altRow ? "#fff" : "#E0EBEB"); height:40px;width:200px; padding-left:20px;"> @item.firstName @item.lastName</td>
                    </tr> 
                    altRow = !altRow;
                }                            
            }
        </tbody>
    </table>    
</div>
...Content...
<div style="border-radius:20px;border:1px groove #E0EBEB; width:520px;height:100px;float:right;margin-top:15px;">
<div style="margin-top:10px;">Task Options</div>
      <div style="margin-top:20px;">
       ===>   <a id="completed"class="btn btn-default" target="_blank" href="@Url.Action("CompletedGrid", "Home")"><i class='fa fa-check fa-fw'></i>Completed</a>
      </div>
</div>

如果我点击我的“已完成”,它将链接到我的“CompletedGrid”

CompletedGrid 视图

@model Maintenance_.Models.CompletedGridModel
@{
    ViewBag.Title = "CompletedGrid";
}
<div id="page-wrapper">
<div class = "row">
    <div class = "col-lg-12">
===>        <h1 class= "page-header"> Completed @*Technician Name*@</h1>    
    </div>
</div>
...Content...

这是我的问题:有没有办法将字符串参数传递给另一个视图?使用这个 @Url.Action("CompletedGrid", "Home") 剃须刀还是应该在我的控制器中编辑/添加一些东西?这样在我的 H1 标签中就可以输出 "Completed parameter"

这是我的控制器和模型。

CompletedGrid 模型

public class CompletedGridModel
{
    public string techNo { get; set; }
    public string taskNo { get; set; }
    public string jobNo { get; set; }
    public string grpNo { get; set; }
    public string assetNo { get; set; }
    public string remarks { get; set; }
    public string status { get; set; }

    public IList<Maintenance_.Models.CompletedGridModel> CompleteList { get; set; }
}

CompletedGrid 控制器

public ActionResult CompletedGrid(/*Parameter*/)
{
        TaskFacade _oTaskFacade = new TaskFacade();
        Maintenance_.Models.CompletedGridModel _oCompleteGridModelMODEL = new Maintenance_.Models.CompletedGridModel();
        IList<Maintenance_.Models.CompletedGridModel> _oCompletedList = new List<Maintenance_.Models.CompletedGridModel>();
        var sample = _oTaskFacade.getTask(_oAppSetting.ConnectionString).ToArray();
        foreach (var test in sample)
        {
            ...data transfer...
        }
        _oCompleteGridModelMODEL.CompleteList = _oCompletedList;
        return View("CompletedGrid", _oCompleteGridModelMODEL);

【问题讨论】:

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


    【解决方案1】:

    您可以像在索引视图中一样传递查询字符串

     <a id="completed"class="btn btn-default" target="_blank" href="Home/CompletedGrid?value=something">
    

    在控制器中你可以得到查询字符串

    public ActionResult CompletedGrid(/*Parameter*/)
    {
    
    ViewBag.from= Request.QueryString["value"];
    .......
    return View("CompletedGrid", _oCompleteGridModelMODEL);
    }
    

    在 CompletedGrid 视图中

    <div class = "col-lg-12">
    ===>        <h1 class= "page-header"> Completed @ViewBag.from</h1>    
        </div>
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      对于 URL,请始终使用 @Url.Action() 而不是 'controller/action'

      <a id="completed"class="btn btn-default" target="_blank" href="@Url.Action("Home","completedGrid")"+"?value=something">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-04
        • 2021-11-09
        相关资源
        最近更新 更多