将HTML文本数据发送到MVC应用程序C#中的ActionLink



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

@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>
    }

该表格应该提供一个" ActionLink",在进行搜索后将单击,我希望动作链接通过先前搜索的字符串" searchString",但在单击链接时,但是它发送了一个空字符串。

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

如何在此" ActionLink"中保留原始搜索字符串"搜索串"的值?两种表格都转到相同的方法Index()

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

请使用此。

@html.action("控制器"," actionName",new {searchstring =" searchstring"}(

另一种方式是这个

var searchstring =" abc"; @html.action("控制器"," ActionName",new {searchstring = 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" 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>

最新更新