繁体   English   中英

PartialView 未在 Asp.net MVC 中显示?

[英]PartialView is not showing in Asp.net MVC?

I have an array which as the number( Id's of some tags ) and I am passing this array to the action method using ajax call when this array passed to the controller action method( RawTagCreation ) and then the numbers in this arrays are used to get来自数据库的数据,然后传递到另一个 ViewModel object ( rawTagVMs ),然后我使用这个 ViewModel object 返回部分视图。 问题是部分视图没有显示? 基本上 ajax 调用的成功事件也不会被触发。

function selectedValues() {
            var datas = [];
            $('#search_to option').each(function () {
                datas.push($(this).attr('value'));
            });
            console.log(datas);
            if (datas.length > 0) {
                $.ajax({
                    url: "@Url.Action("RawTagCreation", "SourceTagMapping")",
                    type: 'POST',
                    contentType: 'application/json',
                    dataType: 'json',
                    data: JSON.stringify({ 'selectedTags': datas }),
                    success: function (data) {
                        debugger;
                        $('#myModalContent').html(data);
                        $('#myModal').modal('show');
                    }
                });
            }
            else {
                alert('Please Select One or More Raw Tags')
            }
        }

动作方法在这里给出

 public ActionResult RawTagCreation(int[] selectedTags)
        {
            List<RawTagVM> rawTagVMs = new List<RawTagVM>();
            if (ModelState.IsValid)
            {
                foreach (var item in selectedTags)
                {

                    var OldSourecTag = db.Real_Raw_Points.Where(x => x.Source_Tag_Id_Fk == item).FirstOrDefault();
                    var OPCTag = db.OPC_SourceTags.Where(x => x.Source_Tag_Id == item).FirstOrDefault();
                    if (OldSourecTag == null)
                    {
                        RawTagVM objToInsertRawTags = new RawTagVM();
                        objToInsertRawTags.Source_Tag_Id_Fk = item;
                        objToInsertRawTags.R_Tag_Name = "Provide Tag Name";
                        rawTagVMs.Add(objToInsertRawTags);

                    }
                }
                return PartialView("_Update_TagNames", rawTagVMs);
            }
            return View();
        }

部分视图在这里

@model List<OmniConnect.ViewModel.RawTagViews.RawTagVM>
<div class="modal-header">
    <h3 class="modal-title" id="exampleModalLabel">Update The Raw Tag Names</h3>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
</div>


<div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
    <button type="button" class="btn btn-primary" onclick="Confirm()">Confirm Update</button>
</div>

当您尝试 ajax 调用时,您无法返回部分视图

如果要返回视图,则必须使用 Html.Beginform 之类的表单并提交

如果你想返回成功方法你必须返回一个值可能是字符串和 append 它到一个标签

 public ActionResult RawTagCreation(int[] selectedTags)
    {
        List<RawTagVM> rawTagVMs = new List<RawTagVM>();
        if (ModelState.IsValid)
        {

            return Json("<div>any view</div");
        }
        return json("");
    }

然后你可以像这样使用:

 $.ajax({
                url: "@Url.Action("RawTagCreation", "SourceTagMapping")",
                type: 'POST',
                contentType: 'application/json',
                dataType: 'json',
                data: JSON.stringify({ 'selectedTags': datas }),
                success: function (data) {
                    debugger;
                    $('#myModalContent').html(data);
                    $('#myModal').modal('show');
                }
            });

暂无
暂无

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

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