質問

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()..

:)

役に立ちましたか?

解決 2

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 ..

他のヒント

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

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top