0

I have this controller:

public ActionResult Index(int id)
        {
            var cust = (from c in dataModel.Customers
                        where (c.MembershipID == id)
                        select c).First();
            return View(cust);
        }

I want to be able to pass through the ID from a text box on the main page. I tried the following but it says 'memberid' does not exist. Any ideas? Thanks.

<asp:TextBox ID="memberid"/>
<%: Html.ActionLink("Customer", "Index", new {id = memberid.Text}) %>

My goal is to Enter a value in a textbox, click a button and then be redirected to a new view showing that users details.

1
  • 1
    You should not use server-side controls (<asp:Whatever>) in MVC. Commented Mar 24, 2011 at 15:03

2 Answers 2

2

This is only possible using Javascript; you can handle the link's click event and explicitly navigate to the URL.

Using jQuery:

$('#link').click(function() { 
    location = "/Customer/Index/" + encodeUriComponent($('#memberId').text());
});

If you want to do it without Javascript, you can make a form containing a textbox and an <input type="submit" />.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, I am getting to grips with how MVC works, would it be possible for a snippet example? Thank you.
Thank you for the snippet, not to be a pain :) but I don't quite understand how to tie this into the application exactly..
0

Since you want the user to click a button, change the link to a button and in the http post method for your main page, Redirect to the Customer/Index view passing in the memberId.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.