MVC3 DropDownList的渲染局部视图和数据持久化 [英] mvc3 dropdownlist to render partial view and persist data

查看:153
本文介绍了MVC3 DropDownList的渲染局部视图和数据持久化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有呈现一个MVC的DropDownList并呈现局部视图的页面,只要在DropDownList的值发生变化。这是正常的工作,直到我在局部视图点击按钮来坚持在局部视图所做的更改。

I am trying to have a page that renders an mvc dropdownlist and renders a partial view whenever the value in the dropdownlist changes. It is working alright until I click the button in the partial view to persist changes made in the partial view.

我有以下的code为我的局部视图 -

I have the following code for my partial view -

<script type="text/javascript">
    $(function () {
        $("#sortable, #sortable2").sortable({
            connectWith:  ".connectedSortable"
        }).disableSelection();        
    });

    $(function () {
        $("button").button();
        $(".submit").click(function () {
            var list = "somedata";
            var companyid = $("#myid").text();            
            $.ajax({
                url: "/Controller/Action",
                data: {
                    'values': list,
                    'id': myid
                }
            });
        });
    });

</script>

......

<div class="submit">
    <button>Submit</button>
</div>

下面是code为我的主视图 -

Here is the code for my main view -

<script type="text/javascript">
    function processList() {
        var myid = $("#ddList").val();
        $.ajax({
            url: 'Controller/Action/',
            data: {
                'id': myid 
            },
            datatype: 'html',
            success: function (result) { success(result); }            
        });
    }

    function success(result) {
        $("#partial").html(result);
    }
</script>

@using (Html.BeginForm("Action", "Controller"))
{      
    @Html.DropDownListFor(x => x.SelectedValue, Model.MyEntity, new { @id = "ddList", onchange = "processList();" })


<div id="partial" style="height:  90%">

        @Html.Partial("_MyPartialView")

</div>
}

从我的控制器code低于 -

The code from my controller is below -

public ActionResult Action(int? id)
        {                        
            id = id ?? 1;    

            var vm = new MyViewModel
            {
                .....
            };
            return PartialView("_MyPartialView", vm);
        }

        public ActionResult PersistActivities(int myid, string values)
        {
            int result = _service.UpdateActivityOrderByCompany(companyid, values);                         
            return new EmptyResult();            
        }

我想,当我点击按钮,坚持在页面重新渲染只是局部视图。即使它只是没有做任何事情,这将是罚款。什么是实际发生的是,该控制器再次进入动作的动作,然后抛出一个渲染错误。如果我继续通过jQuery的错误,只是局部视图的数据呈现在整个窗口。

I want the page to re-render just the partial view when I click the persist button. Even if it just didn't do anything, that would be fine. What is actually happening is that the controller is entering the "Action" action again and then throws a rendering error. If I continue through the jquery error, just the partial view data renders in the whole window.

任何人都可以指出我在哪里的问题与渲染/控制器电话?

Can anyone point out where my issues are with rendering/controller calls?

我希望这是足够的信息。我不得不裁减了code,但如果需要可以提供更多。

I hope that is enough info. I had to trim down the code, but can provide more if necessary.

感谢您的任何想法。

EDTI -

我得到的错误是在第一个函数的局部视图渲染的排序列表 -

the error I'm getting is in the partial view on the first function for rendering the sortable lists -

微软JScript运行时错误:预期的对象。

Microsoft JScript runtime error: Object expected

希望帮助

推荐答案

有一些事情,可能是发生了什么事的原因。

There are a number of things that could be the cause of what's happening.

您的网址可能是不正确的。

Your urls are probably not correct.

url: 'Controller/Action/',

应该是:

url: '@Url.Action("Action", "Controller")', 

您按钮提交code指的是你的动作控制器,但你可能希望它去你的 PersistActivities 。此外,您的控制器动作期待的变量身份识别码,但是你发送 ID 和值而不是。你也提到一个HTML元素身份识别码,但它这里没有显示,我假设你离开它的简洁。目前也没有一个成功的功能 - 你有什么打算该按钮真正做到

Your button submit code is referring to your Action in your Controller, but you probably want it to go to your PersistActivities. Also, your controller action is expecting variables myid and values, but you're sending id and values instead. You're also referring to an html element myid, but it's not shown here, I assume you left it out for brevity. There also is not a success function - what are you intending for that button to actually do?

你可能需要的是这样的:

What you probably want is something like this:

 $(".submit").click(function () {
     var list = "somedata";
     var myid = 1;
     alert(myid);
     $.ajax({
        url: '@Url.Action("PersistActivities", "Home")',
        data: {
           'values': list,
           'myid': myid
        }            
     });
  });

最后,您的按钮被张贴页的理由是,它提交表单。你应该改变你的按钮的类型为类型的按钮,如果你不希望它实际上是提交表单。

Finally, the reason your button is posting the page is that it's submitting the form. You should change the type of your button to be type button, if you don't want it to actually submit the form.

<button type="button">Submit</button>

这篇关于MVC3 DropDownList的渲染局部视图和数据持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆