在MVC应用程序中将Html文本数据发送到ActionLink,c#

时间:2018-02-15 09:51:16

标签: c# html asp.net-mvc html5 asp.net-mvc-4

此表单搜索传入" searchString"的值。到表中并返回所有选定的值。

@using (Html.BeginForm())
    {
    @Html.ValidationSummary(true)
    <fieldset>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="text" name="searchString" />
                <input type="submit" value="Search" class="btn btn-default" />
            </div>
        </div>
    </fieldset>
    }

这个表格应该提供一个&#34; ActionLink&#34;在搜索完成后将被点击,我希望操作链接传递先前搜索的字符串&#34; searchString&#34;,当点击链接但发送空字符串时。

<table class="table">
    <tr>
        <th>
            @Html.ActionLink("Author", "Index", "Home", new { searchString = Html.Name("searchString").ToString()}, null)
        </th>
    </tr>
</table>

如何保留原始搜索字符串的值&#34; searchString&#34;在这个&#34; ActionLink&#34;?两种形式都采用相同的方法Index()

3 个答案:

答案 0 :(得分:0)

&#13;
&#13;
function MyFunc()
{
if (($('#searchString').val()).length > 0) {
 var url = "/Home/Index?SearchTerm=" + $('#searchString').val(); 
            window.location.href = url; 
}      
else
{
alert('please enter value for search');
}
           
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="text" name="searchString" id="searchString" />
                <input type="submit" value="Search" class="btn btn-default" />
            </div>
        </div>
    </fieldset>
    

<table class="table">
    <tr>
        <th>
          <a href onclick="MyFunc()">Search</a>  
    </tr>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

请使用此。

@ Html.Action(&#34; Controller&#34;,&#34; actionName&#34;,new {searchString =&#34; searchString&#34;})

另一种方式是这个

var searchString =&#34; abc&#34 ;;  @ Html.Action(&#34; Controller&#34;,&#34; actionName&#34;,new {searchString = searchString})

答案 2 :(得分:0)

你可以通过你的控制器通过like变量发送它:

@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="text" name="searchString" value="@ViewBag.SearchString"/>
            <input type="submit" value="Search" class="btn btn-default" />
        </div>
    </div>
</fieldset>
}

你的控制器应该是这样的

 public ActionResult Search(string searchString)
    {

        ViewBag.SearchString = searchString; // This line send your string back to your view

        return View();
    }

链接同样适用

<table class="table">
<tr>
    <th>
        @Html.ActionLink("Author", "Index", "Home", new { searchString = ViewBag.SearchString}, null)
    </th>
</tr>