在链接点击上将值从一个视图发布到另一个视图

时间:2012-07-04 10:26:30

标签: asp.net-mvc-3 razor

我有两个视图,即Index视图和Createdialog视图。 索引视图的代码是:

<p>Search For: &nbsp;</p>
@Html.TextBox("companyName", Model);

和Createdialog视图的代码是:

@foreach(var item in Model.branch)
{    

    <tr>
    <td><a href="#">   
    @Html.DisplayFor(itemModel => item.companyName)
    </a></td>
    <td>@Html.DisplayFor(itemModel => item.branchName)</td>
    <td>@Html.DisplayFor(itemModel => item.address)</td>
    <td>
    @Html.ActionLink("Delete", "Delete", new { id = item.branchId })
    @Html.ActionLink("Select", "Index", new { id = item.companyName })
   </td>
</tr>      
}

现在我要做的是,将公司ID的值从createdialog视图发送到索引对话框视图,并在单击选择链接时在文本框中显示companyName。提供建议......谢谢。

2 个答案:

答案 0 :(得分:0)

您不会在MVC中从一个视图“发送值”到另一个视图。相反,您将从View向Controller操作发送数据,然后显示另一个View。

你所拥有的是近在咫尺,但你想在Controller中做的是接受companyName作为输入参数,如下所示:

[HttpGet]
public ActionResult Index(string id)
{
    // Perform any initialization or other operations

    // Show the Index view with the id (the companyName) as the Model
    return View("Index", id);
}

答案 1 :(得分:0)

在你看来你应该得到这样的东西:

而不是

<td><a href="#">   
    @Html.DisplayFor(itemModel => item.companyName)
</a></td>

使用

@Html.ActionLink(item.companyName, "Index", new { name = item.companyName })

使用

在控制器中捕获它
[HttpGet]
public ActionResult Index(string name)
{
    return View("Index", name);
}
祝你好运:)

相关问题