繁体   English   中英

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

[英]Send Html text data to ActionLink in MVC application, c#

此表单在表中搜索传递给“ 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>
    }

此表单应该提供一个“ ActionLink”,将在进行搜索后将其单击,并且我希望动作链接在单击链接但传递空字符串时传递先前搜索的字符串“ searchString”。

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

如何在此“ ActionLink”中保留原始搜索字符串“ searchString”的值? 两种形式都使用相同的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(“ Controller”,“ actionName”,新的{searchString =“ searchString”})

另一种方式是

var searchString =“ abc”; @ Html.Action(“ Controller”,“ actionName”,新的{searchString = searchString})

您可以通过控制器通过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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM