Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用ajax将数据从控制器传递到视图_Jquery_Asp.net_Ajax_Asp.net Mvc_Razor - Fatal编程技术网

Jquery 使用ajax将数据从控制器传递到视图

Jquery 使用ajax将数据从控制器传递到视图,jquery,asp.net,ajax,asp.net-mvc,razor,Jquery,Asp.net,Ajax,Asp.net Mvc,Razor,嗨,我从数据库中选择数据,我希望这些数据通过ajax从我的控制器传递到视图,但它不起作用 你能帮我吗 这是我的控制器: [HttpGet] public ActionResult Foo(string email_uzivatele) { var person = AdvertServiceLayer.Instance.SelectByEmail(email_uzivatele); return Json(perso

嗨,我从数据库中选择数据,我希望这些数据通过ajax从我的控制器传递到视图,但它不起作用

你能帮我吗

这是我的控制器:

[HttpGet]
        public ActionResult Foo(string email_uzivatele)
        {
            var person = AdvertServiceLayer.Instance.SelectByEmail(email_uzivatele);
            return Json(person, JsonRequestBehavior.AllowGet);
        }
这里我想显示我从数据库中选择的电子邮件:

 <input id="email" name="email_uzivatele" type="text" class="form-control input-md">
        public List<Advert> SelectByEmail(string email_uzivatele)
                {



                    string queryString = "SELECT distinct email_uzivatele from 

    Reklama 
    where email_uzivatele like '%" + @email_uzivatele + "%'";
                    // Create the Command and Parameter objects.
                    SqlCommand command = new SqlCommand(queryString, Connection);

                    command.Parameters.AddWithValue("@email_uzivatele", "");

                    // Open the connection in a try/catch block. 
                    // Create and execute the DataReader, writing the result
                    // set to the console window.
                    try
                    {

                        SqlDataReader reader = command.ExecuteReader();
                        List<Advert> advert = new List<Advert>();
                        while (reader.Read())
                        {

                            Advert a = new Advert();
                            a.email_uzivatele = reader[0].ToString();



                            Console.WriteLine("email_uzivatele: " + " " + "
 " + a.email_uzivatele);

                            advert.Add(a);
                        }

                        reader.Close();
                        return advert;
                    }
                    catch (Exception ex)
                    {
                        chyba.zapsat_do_souboru(ex.Message);
                        Console.OpenStandardOutput();
                        Console.WriteLine(ex);
                        //zalogovat chybu
                        return null;
                    }


                }
以下是从数据库中选择电子邮件的功能:

 <input id="email" name="email_uzivatele" type="text" class="form-control input-md">
        public List<Advert> SelectByEmail(string email_uzivatele)
                {



                    string queryString = "SELECT distinct email_uzivatele from 

    Reklama 
    where email_uzivatele like '%" + @email_uzivatele + "%'";
                    // Create the Command and Parameter objects.
                    SqlCommand command = new SqlCommand(queryString, Connection);

                    command.Parameters.AddWithValue("@email_uzivatele", "");

                    // Open the connection in a try/catch block. 
                    // Create and execute the DataReader, writing the result
                    // set to the console window.
                    try
                    {

                        SqlDataReader reader = command.ExecuteReader();
                        List<Advert> advert = new List<Advert>();
                        while (reader.Read())
                        {

                            Advert a = new Advert();
                            a.email_uzivatele = reader[0].ToString();



                            Console.WriteLine("email_uzivatele: " + " " + "
 " + a.email_uzivatele);

                            advert.Add(a);
                        }

                        reader.Close();
                        return advert;
                    }
                    catch (Exception ex)
                    {
                        chyba.zapsat_do_souboru(ex.Message);
                        Console.OpenStandardOutput();
                        Console.WriteLine(ex);
                        //zalogovat chybu
                        return null;
                    }


                }
public List SelectByEmail(字符串email\u uzivatele)
{
string queryString=“选择不同的电子邮件\u uzivatele from”
雷克拉马
其中email_uzivatele类似“%”+@email_uzivatele+“%”;
//创建命令和参数对象。
SqlCommand=newsqlcommand(queryString,Connection);
command.Parameters.AddWithValue(“@email\u uzivatele”,”);
//在try/catch块中打开连接。
//创建并执行DataReader,写入结果
//设置为控制台窗口。
尝试
{
SqlDataReader=command.ExecuteReader();
列表广告=新列表();
while(reader.Read())
{
广告a=新广告();
a、 email_uzivatele=阅读器[0]。ToString();
Console.WriteLine(“电子邮件地址:+”“+”
“+a.email_uzivatele);
广告.加入(a);
}
reader.Close();
返回广告;
}
捕获(例外情况除外)
{
chyba.zapsat_do_souboru(例如信息);
Console.OpenStandardOutput();
控制台写入线(ex);
//扎洛戈瓦特奇布
返回null;
}
}

我认为问题来自url属性。其他一切似乎都是正确的。我向您推荐的更好的方法是使用Ajax html helper。我向您展示了-ajaxhelper和jquery的示例

Ajax助手方法->我更喜欢它,因为MVC视图保持干净,而JS不会从不同的文件发出一些请求

@using (Ajax.BeginForm("AddToArticle", "Comments", null, new AjaxOptions
                                                                     {
                                                                         HttpMethod = "POST",
                                                                         InsertionMode = InsertionMode.InsertBefore,
                                                                         UpdateTargetId = "comments-list",
                                                                         OnSuccess = "acceptedComment",
                                                                         OnFailure = "rejectedComment"
                                                                     }))
            {
                @Html.AntiForgeryToken()

                <input type="hidden" value="@Model.Id" name="toId"/>
                <div class="row bottom-margin">
                    <div class="col-md-12">
                        <textarea id="comment-area" class="form-control" placeholder="Коментар" rows="5" name="content"></textarea>
                    </div>
                </div>
                <div>
                    <input type="submit" class="show-more cat-sports" title="Post comment" value="Добави"/>
                </div>
            }
我希望这能解决你的问题。
致以最良好的祝愿

我再次阅读了你的问题,我发现你的概念是错误的。我将在接下来的几行中解释我的意思。所以

不存在通过ajax从控制器传递数据的情况。使用ajax只能将数据从视图传递到控制器。它用于SPA功能的情况。如果要加载数据并在视图中显示,则应在MVC中使用视图模型并直接在视图中加载

我已经为您制作了一个使用ajax的工作示例,我将向您解释。首先,您还没有调用您的函数

在这个例子中,我举了一个简单的例子来告诉你这个想法是什么

这是控制器

    [HttpGet]
    public ActionResult Foo(string email_uzivatele)
    {
        return Json(email_uzivatele, JsonRequestBehavior.AllowGet);
    }
这些是html输入

<input id="email" name="email_uzivatele" type="text" class="form-control input-md">
<input id="submit-btn" type="submit" value="Sumbit" />

在这种情况下,您可以通过提交按钮上的ajax将您在文本框中键入的值传递到控制器中,单击检查值是否正确。保持警报警报(person)和警报(person.email_uzivatele),并查看值是否正确,是否发布了绑定。添加警报后,什么都没有发生。说真的,我做错了什么?如果您的
SelectByEmail
函数返回一个列表。。那么您的ajax成功应该使用索引来获取值
$('#email').val(person[0].email_zivatele)即使我在我的ajax中更改了您建议的内容,但仍然不起作用您仍然没有告诉我们哪些内容不起作用。。控制器操作是否被击中。。在归还之前,此人是否有任何记录。。email_uzivatele是否包含值?PS:您还可以使用缩写语法-$.post(“ajax/test.html”,function(data){$(“.result”).html(data);});我将我的url更改为url:“/Home/Foo”,但它仍然不起作用,我不知道如何在我的案例中使用Ajax html helper谢谢,但我问了这个问题,因为我需要从数据库中选择数据,然后我想使用Ajax和google图表在图表中显示这些数据:。但是我不知道怎么做。你知道怎么做吗?
  $(function () {

    $('#submit-btn').on('click', function () {

        var email_uzivatele = $('#email').val();

        $.ajax({
            url: '/Home/Foo',
            type: 'GET',
            dataType: 'json',
            cache: false,
            data: { email_uzivatele: email_uzivatele },
            success: function (person) {
                $('#email').val(person.email_uzivatele);
            }
        });
    });
});