0

I have the following idea that i am trying to implement

@foreach (var item in Model)
{
<div>User: @item.Name<br />
    Scores: @item.scores<br />
    @Html.TextBox("lastvisit");
    @Html.ActionLink("Update item", "updateMyItem", new  { name = item.Name, lastvisit=?????  })
    </div>
}

I have seen this SO question Pass text in query string, but that is not what i want..

so my question is .. in the above code how can I replace the (?????) with the value of the textbox(lastvisit) and send the value as a querysting in the URL of the action link ??

Notice that I opted not to use a webform for my own reason and I know how to do it with webform.submit(), but my main concern is how to extract the value of @HTMLhelper.textbox()..

:)

3
  • Ok after asking , i think i figure that is impossible almost.. bec, the actionlink creation time will be on the server, and the user didn't write anything in the textbox yet... and once the page is sent to the user there is no way to manipulate the actionLink except using javascript :( Commented Mar 23, 2014 at 9:18
  • You can always take advantage of jquery $(document).ready. You can pick up the value in the text box and append it to the action link Commented Mar 27, 2014 at 6:32
  • Can u answer it down with code?? Because I think it is impossible Commented Mar 27, 2014 at 6:48

3 Answers 3

1

Something like this might help. For this to work you need to render unique IDS for the links and textboxes.

Here is an example

Action method with a simple model

public ActionResult Index(int? id)
{
    List<MyModel> mod = new List<MyModel>() { 
        new MyModel { SelectedValue = 1 } ,
        new MyModel {SelectedValue = 2},
        new MyModel {SelectedValue = 3}
    };
        return View(mod);
}

And this is the view with the script.

@model List<MVC3Stack.Models.MyModel>
@{
    ViewBag.Title = "Home Page";
    var i = 1;    
}
<h2>@ViewBag.Message</h2>
<script type="text/javascript">
    $(document).ready(function () {
        var lastVisits = $("input[id*='lastVisit']");
        $(lastVisits).each(function () {
            var i = this.id.substring(this.id.length - 1);
            var link = $("[id='testLink" + i + "']");
            if (link) {
                var _href = $(link).attr("href");
                $(link).attr("href", _href + "&lastvisit=" + $(this).val());
            }
        });
    });

</script>
@foreach (var item in Model)
{
    @Html.TextBox("lastVisit" + i, item.SelectedValue )
    @Html.ActionLink("TestLink", "Index", "Home", new { id = "testLink" + i });
   <br />
   i++;
}
<input type="button" value="GetFile" id="getFile" />

here is a snapshot with the changed link

Sample output

Hope this helps.

EDIT

My bad. Here is the update javascript which can do the trick.

$(document).ready(function () {
    var lastVisits = $("input[id*='lastVisit']");

    $(lastVisits).each(function () {
        $(this).change(function () {
            var i = this.id.substring(this.id.length - 1);
            var link = $("[id='testLink" + i + "']");
            if (link) {
                var _href = $(link).attr("href");
                $(link).attr("href", _href + "?lastvisit=" + $(this).val());
            }
        });
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

You showed that JavaScript is the ultimate solution, however the solution you provided didn't solve my problem above, I will vote you up for the effort: )
What is it that you want? I thought that you want to pass the value from text box to the action link! May be I did not understand the question :(
Read my question above and tell me how can I submit the values of text boxes in a querystring using action link
0

Ok Nilesh I will answer my own question.. but I will cheat from your solution lol cuz it is inspiring .. thanx in advance

<script type="text/javascript">
    $(document).ready(function () {
        var myMainPath = "updateMyItem";

        $("a").each(function(){
          var name =$(this).parent("div").child("#itemName").val();
          var visit = $(this).parent("div").child("#lastvisit").val();
          $(this).attr('href', myMainPath +'?name=' + name + '&lastVisit='+ visit);


        });
    });

</script>

@foreach (var item in Model)
{
<div>User: <span id="itemName">@item.Name</span><br />
    Scores: @item.scores<br />
    @Html.TextBox("lastvisit", new { id="lastvisit"});
    <a  href=""> Update item </a>

    </div>
}

you see it can be done by javascript , but i was mistaken to think that you can manipulate it via Razor on the server ..

2 Comments

manipulating through Razor wont be possible cause you dont know what value you have in the text box when the Link is clicked. Its good that you answered your own question, you might as well mark it as answer so it would help someone else :)
that was my point in the first comment on my question: i stated that i realised that the question is impossible to do through Razor on the server and my only tool to do the job is client side Javascript :) however you helped me allot to answer it (although not tested yet)..regards :)
0

I know this post is old, but i just started learning MVC thanks to the asp.net/mvc/ website and i faced a similar problem during the tutorial. My Index action expects 2 parameters which define sorting and filtering (through the macthing of a substring) of a set of record displayed in the view. My problem is that i can't sort a filtered subset, since the view is called but no parameter for filtering is passed once i activate the sorting clicking on the link of the header.

@* Index.cshtml *@
    @using (Html.BeginForm())
    {
      <p>
      Find by name: @Html.TextBox("SearchString")
      <input type="submit" value="Search" />
      </p>
    }
    . . .
    <!-- header -->
    <table><tr><th>
      @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm })
    </th>

. . .

//controller.cs
    public ActionResult Index(string sortOrder, string searchString){...}

I thought i needed to access the TextBox, but apparently i just need to use the provided ViewBag object as already seen in this example!

@* Index.cshtml *@
    @using (Html.BeginForm())
    {
      <p>
      Find by name: @Html.TextBox("SearchString")
      <input type="submit" value="Search" />
      </p>
    }
    . . .
    <!-- header -->
    <table><tr><th>
      @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm, searchString = ViewBag.SearchString })
    </th>

. . .

//controller.cs
    public ActionResult Index(string sortOrder, string searchString)
    {
      ViewBag.SearchString = searchString;
      . . .  
    }

Maybe a similar behaviour could have been used for solving the problem that originated this post, i don't know.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.