https://www.c-sharpcorner.com/article/passing-multiple-models-using-ajax-in-asp-net-mvc-step-by-step/
https://blog.csdn.net/fynjy/article/details/24498527
https://www.cnblogs.com/cnhonker/p/6912352.html
https://www.dotnetcurry.com/ShowArticle.aspx?ID=384
https://www.cnblogs.com/Leo_wl/p/3231546.html
一、使用ajax实现view向controller传值
1、在对应HTML标签的onclick事件中写以下函数。(例如,我使用的是img标签)
<pre code_snippet_id="312616" snippet_file_name="blog_20140425_2_7560556" name="code" class="html">
<input border = "0" name="search" type="text" id="search" value="colon"/>
<img οnclick="my_test_search();" src = "@Url.Content("~/Source/img/SearchPages/search-icon.gif")" alt="" />
<script type="text/javascript">
function my_test_search() {
var searchContent = document.getElementById('search').value;
$.ajax({
url: '@Url.Action("ClinicSearchResult","SearchPages")',
type: 'GET',
data: { searchContent: searchContent },
success: function () {
alert(searchContent);
window.location.href = '@Url.Action("ClinicSearchResult","SearchPages")';
},
error: function () {
alert("error");
}
});
alert("test");
}
</script>
```
代码说明:其中url.Action中的第一个参数,是值searchContent要传向的页面,SearchPages是对应controller的名字。data中,前一个searchContent是要后台要接收这个参数的变量名字。
controller中的代码如下:
public ActionResult ClinicSearchResult(string searchContent)
{
//利用搜索条件,在该函数中写搜索语句以及显示搜索结果
string get_concept = string.Format("select concept_name,cui,semantic_type from crc_clinical_concept where concept_name like '%{0}%' and status = '1'", searchContent);
DataTable dt = dataoperate.GetTable(get_concept);
//Response.Redirect("/Views/SearchPages/ClinicSearchResult");
return View(dt);
//ViewData["concept1"] = Request.Form["mysearch"];
//return View();
}
二、使用ajax实现后台到前台的传值
写在view中的代码:
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$("#btnSub").click(function () {
$.ajax({
type: "POST",
url: "/Home/test1",
data: "",
success: function (sesponseTest) {
$("#txt1").val(sesponseTest);
}
});
});
});
</script>
<input type="text" id="txt1" name="txt1" />
<input type="button" id="btnSub" name="btnSub" value="调用Action"/>
写在controller中的代码
public ActionResult test1()
{
return Content("返回一个字符串");
}