weixin_33744854 2013-01-17 13:04 采纳率: 0%
浏览 30

使用Ajax渲染MVC视图

                <div class="grid--cell fl1 lh-lg">
                    <div class="grid--cell fl1 lh-lg">
                        It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and   cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened,   <a href="/help/reopen-questions">visit the help center</a>.

                    </div>
                </div>
            </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2013-01-18 16:42:40Z" class="relativetime">7 years ago</span>.</div>
        </div>
    </aside>

The task I am trying to accomplish basically involves me using AJAX to call an MVC view, and render the content of the view into my main content div. I understand that as I won't be doing a full page reload, just a refresh on the content I need, I will need to be altering the URL string to account for whichever controller and action I am attempting to call, to get the required view. I havent done a lot of Ajax as of yet, as I am only a junior developer, however I am very interested in learning how to achieve things such as this, and anything that follows.

I am using C# as my primary language, and can use either mvc3 or mvc4, whether or not that makes much difference, I am not sure.

</div>
  • 写回答

3条回答 默认 最新

  • weixin_33671935 2013-01-17 13:14
    关注

    Button: @supplierQuoteId is from the model via Razor.

    <a id="@supplierQuoteId" class="t-button" style="float: right; margin: 3px; padding: 2px; line-height: 0;" 
        onclick="ajaxEditSupplierQuote(this.id)">
        <span class="t-icon t-edit">Edit</span>
    </a>  
    

    JavaScript: If $.ajax is complete, I open a Telerik Window and there is a .Content - div with the id="supplierquote-dialog-contet" which is then filled with the returned PartialView from the Controller, otherwise, i set .html(" ") . Empty.

    <script type="text/javascript">        
        function ajaxEditSupplierQuote(id) {
            var strUrl = "/SupplierQuote/Edit" + "/" + id.toString();
            $.ajax({
                type: "GET",
                url: strUrl,
                dataType: "html",
                success: function (data) {
                    if (!data) {
                        $('#supplierquote-dialog-content').html(" "); // error check
                    }
                    else {
                        $('#supplierquote-dialog-content').html(data);
                    }
                },
                complete: function () {
                    var window = $("#SupplierQuoteDialog").data("tWindow");
                    window.center().open();
                }
            });
        }
    </script>
    

    SupplierQuoteController:

        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            SupplierQuoteEntity supplierQuote = _supplierQuoteRepository.GetById(id);
            return PartialView("Edit", supplierQuote);
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集