在ASP.NET MVC中从AJAX调用ActionResult时,视图未加载

时间:2018-04-03 18:02:36

标签: asp.net asp.net-mvc asp.net-mvc-4 razor

单击按钮后,我使用import tensorflow as tf # v.1.4 import sys # Predetermine minibatch size. num_per_class = 6 # Create example datasets. ds0 = tf.data.Dataset.range(0, 100, 2) ds1 = tf.data.Dataset.range(1, 101, 2) # Minibatchify. Note: could use adjustable tensor for minibatch size. ds0 = ds0.apply(tf.contrib.data.batch_and_drop_remainder(num_per_class)) ds1 = ds1.apply(tf.contrib.data.batch_and_drop_remainder(num_per_class)) # Run forever. ds0 = ds0.repeat() ds1 = ds1.repeat() # Dataset iterators. ds0_itr = ds0.make_initializable_iterator() ds1_itr = ds1.make_initializable_iterator() # Switcher handle placeholder, iterator and ultimate minibatch datums. switcher_h = tf.placeholder(tf.string, shape=[]) switcher_h_itr = tf.data.Iterator.from_string_handle(switcher_h, ds0.output_types, ds0.output_shapes) mb_datums = switcher_h_itr.get_next() # Start session. sess = tf.Session() # Dataset iterator handles. ds0_h = sess.run(ds0_itr.string_handle()) ds1_h = sess.run(ds1_itr.string_handle()) # *Separate* dataset feed_dicts. ds0_fd = {switcher_h: ds0_h} ds1_fd = {switcher_h: ds1_h} # Initialize dataset iterators. sess.run([ds0_itr.initializer, ds1_itr.initializer]) # Print some datums from either (XOR) dataset. print('ds0 data: {}'.format(sess.run(mb_datums, ds0_fd))) print('ds1 data: {}'.format(sess.run(mb_datums, ds1_fd))) # DESIRE A MINIBATCH OF SIZE 12: 6 FROM EACH. sys.exit() ds01_fd = {switcher_h: OP_TO_COMBINE_STRING_HANDLES(ds0_h, ds1_h)} print('ds0+ds1: {}'.format(sess.run(mb_datums, ds01_fd))) actionresult调用JavaScript函数:

AJAX

在UploadController.cs中:

 <script type="text/javascript">
        $('.btn-SelectStudent').on('click', function () {
            $('input:radio').each(function () {
                if ($(this).is(':checked')) {
                    var id = $(this).val();
                    $.ajax({
                        url: '@Url.Action("ParseSearchLists", "Upload")',
                        data: { studentId: id }
                    }).success(function (data) {
                       alert('success');
                    });
                }
                else {
                    // Or an unchecked one here...
                }
            });

            return false;
        })
    </script>

在UploadFile()中,我返回了View,它应该加载另一个视图。但我只获得成功&#34;在警报但没有加载新视图。我假设,应该加载视图。

2 个答案:

答案 0 :(得分:0)

看来,你没有在ajax成功时加载返回div的视图。在下面的代码中将代码中的div替换为#divname。我希望这有助于解决您的问题

 <script type="text/javascript">
            $('.btn-SelectStudent').on('click', function () {
                $('input:radio').each(function () {
                    if ($(this).is(':checked')) {
                        var id = $(this).val();
                        $.ajax({
                            url: '@Url.Action("ParseSearchLists", "Upload")',
                            dataType: "html",
                            data: { studentId: id }
                        }).success(function (data) {
                          $('#divName').html(data); // note replace divname with your actual divname
                        });
                    }
                    else {
                        // Or an unchecked one here...
                    }
                });

                return false;
            })
        </script>
      [HttpPost]
        public ActionResult ParseSearchLists(int studentId)
        {
            SearchModel searchModel = ApplicationVariables.searchResults.Where(x => x.StudentId == studentId).ToList().First();                
            return View("UploadFile",searchModel); //debug point hits here.
        }

答案 1 :(得分:0)

你正在制作一个&#34; AJAX&#34;调用服务器,意味着您的请求在当前页面请求之外运行,结果将返回到您的success延续例程,而不是浏览器的呈现引擎。基本上,该例程的data参数可能是您UploadFile视图的整个HTML响应。

这不是.ajax的用途。它用于向服务器发出异步请求,并将数据(通常是JSON或XML)返回到您的javascript,以便在页面上进行评估和显示(这是最常见的用途)。

我无法看到您的HTML,但仅仅使用<a>锚点(链接)标记并在查询字符串上发送您的学生ID是不是更好?除了你的观点之外,很难说出你想要做什么。 HTML(.cshtml)永远不会使用您现在拥有的代码显示。

相关问题