【IT】MVC架构中使用ajax实现view和controller之间值的传递

https://www.c-sharpcorner.com/article/passing-multiple-models-using-ajax-in-asp-net-mvc-step-by-step/

https://blog.youkuaiyun.com/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("返回一个字符串");
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值