Suraj Shrestha
Suraj Shrestha

Reputation: 1808

posting value from one view to another on the link click

I've got two views namely Index view and Createdialog view. The code for index view is:

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

and the code for Createdialog view is:

@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>      
}

Now I want to do is, to send the value of company id from createdialog view to index dialog view and show the companyName in the textbox when i click the select link. Provide suggestion... thank you.

Upvotes: 0

Views: 3987

Answers (2)

Anthony
Anthony

Reputation: 427

in your view u should get something like this:

instead of

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

use

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

capture it in the controller with

[HttpGet]
public ActionResult Index(string name)
{
    return View("Index", name);
}

Good luck :)

Upvotes: 1

Garrett Vlieger
Garrett Vlieger

Reputation: 9494

You don't "send values" from one View to another in MVC. Instead, you will send data from the View to a Controller action and then display another View.

What you have is close, but what you'll want to do in the Controller is to accept the companyName as an input parameter like this:

[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);
}

Upvotes: 0

Related Questions