0

I have this table

 <% using(Html.BeginForm("ViewTwo","Order"))
    {  %>

<table id="Products" class="Products">
    <tr>
        <th>ProductId</th>
        <th>Productname</th>
        <th>Quantity</th>
        <th>UnitPrice</th>
    </tr>
    <% for(int i=0; i < Model.NorthOrderDetails.Count; i++)
       {
           %>
            <tr>
        <td><%: Html.Label(Model.NorthOrderDetails[i].ProductID.ToString()) %></td>
        <td><%: Html.Label(Model.NorthOrderDetails[i].ProductName) %> </td>
        <td><%: Html.TextBoxFor(m => m.NorthOrderDetails[i].Quantity) %></td>
        <td><%: Html.TextBoxFor(m => m.NorthOrderDetails[i].UnitPrice) %></td>
    <td>
        <%:  @Html.ActionLink("Go", "ViewTwo", "Order", new { firstval = Model.NorthOrderDetails[i].ProductID.ToString()}, null)%>
    </td></a></td>
    <td> <input type="submit"> </td> </tr>

         <% } %>
       </table>
     <% } %>

How I can get values from model to controller after I press ActionLink ?

Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54
user4523894
  • 143
  • 1
  • 1
  • 10

1 Answers1

0

I hope I did get you right. You want to access values set as RouteValues in your Html.ActionLink(..) (see MSDN) call? If I am right - this may explain it a bit.

I set up a HomeController

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
    public ActionResult SampleAction(int sampleValue)
    {
        Debug.WriteLine("SampleAction");
        Debug.WriteLine("\tsampleValue: " + sampleValue);

        return View("Index");
    }
}

Afterwards I created an according index view

@{
    ViewBag.Title = "Index";
}

<h2>Home</h2>

<!-- routeValues, HtmlAttributes -->
@Html.ActionLink("Click Me", "SampleAction", "Home",new { sampleValue = 1}, null)

Html page

note the link text!

As you can see, I set a sampleValue parameter as my routeValues. Within my controller I can access this value as it is automatically matched to my sampleValue parameter in my SampleAction.

I printed it out for demo purposes. In case I misunderstood you - please let me know. For more details on routing there are several articles available (eg. see Routing Basics)

Update as of given details

If you want to add specific data to a model sent back to the server you may add your additional data to the routeValues of the form. (see demo code below - will have to be modified to work for your purposes!!)

<!-- only one submit button -->
@using (Html.BeginForm("actionName", "controllerName", new { sampleValue = 1 }, FormMethod.Post))
{
    for (int i = 0; i < 10; i++)
    {
    @Html.TextBoxFor(x => x.NorthOrderDetails[i].UnitPrice)
    }

    <input type="submit" />
}

<!-- each row is one form -->
@for (int i = 0; i < 10; i++)
{
    using (Html.BeginForm("actionName", "controllerName", new { sampleValue = 1 }, FormMethod.Post))
    {
        @Html.TextBoxFor(x => x.NorthOrderDetails[i].UnitPrice)
        <input type="submit" />
    }
}

If you want to format your submit-button looking like a link you may have a look at this corresponding SO question.

Community
  • 1
  • 1
Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54
  • Thanks. This method shows how I can sent specific values from ActionLink. But I want to sent all model from ActionLink or collection of my Model. – user4523894 Mar 03 '15 at 20:16
  • + specific data I want to sent – user4523894 Mar 03 '15 at 20:23
  • you can send values from client two server by posting a form, or by sending them as html parameters (as routeValues do). So maybe you ment to submit the form by clicking a link (instead of a submit button)? – Pilgerstorfer Franz Mar 03 '15 at 20:27
  • @user4523894. You have a submit button post all the form values. You should not pass the model to a GET method (apart from the ugly query string it creates, you will possibly exceed the query string character limit and throw an exception,especially if its collection). Pass an ID property to the GET method and use that to get the object/collection from the repository again. –  Mar 03 '15 at 22:33