English 中文(简体)
Refresh a ListBox in ASP. NET MVC 2 with j Query
原标题:Refresh a ListBox in ASP.NET MVC 2 with jQuery

我仔细研究一下SO,看看看是否曾问过此事,是否找不到任何东西(因此,如果在我道歉之前确实曾问过)。

在我试图做些什么时,如果用户想要掌握的技能在名单上,那么他们就可以在数据库中添加这种技能,那么用户可以从他们的技能清单中挑选,而我用WCF &、j Query AJAX完成了这项工作。 这部法典:

$("#AddNewSkill").click(function () {
    $("#AddNewSkill").attr("disabled", true);
    $("#newSkill").attr("disabled", true);
    var newSkill = $("#newSkill").val();
    var data = { name: $("#newSkill").val(), description: "", type: "Skill" };
    data = JSON.stringify(data)
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "../WeddingPhotographerService.svc/AddNew",
        data: data,
        dataType: "json",
        success: function () {
            successCall( #newSkill ,  #AddNewSkill );
            //$( #SkillListViewContainer ).load( ../Account/GetSkillControl );
        },
        error: function (msg) {
            $("#AddSkillError").text(msg.d);
            $("#AddSkill").attr("disabled", false);
            $("#NewSkill").attr("disabled", false);
        }
    });
});

此处采用AJAX-Enabled WCF服务的方法:

[OperationContract]
public bool AddNew(string name, string description, string type)
{
    switch (type)
    {
        case "":
            goto default;
        case "skill":
            IRepository<Skill> skillRepo = ObjectFactory.GetInstance<IRepository<Skill>>();
            var skill = new Skill { Name = name, Description = (string.IsNullOrEmpty(description)) ? string.Empty : description };
            skillRepo.Save(skill);
            return true;
        case "equipment":
            IRepository<Equipment> eqRep = ObjectFactory.GetInstance<IRepository<Equipment>>();
            var eq = new Equipment { Name = name, Description = (string.IsNullOrEmpty(description)) ? string.Empty : description };
            eqRep.Save(eq);
            return true;
        case "occasion":
            IRepository<Occassion> occRepo = ObjectFactory.GetInstance<IRepository<Occassion>>();
            var oc = new Occassion { Name = name, Description = (string.IsNullOrEmpty(description)) ? string.Empty : description };
            occRepo.Save(oc);
            return true;
        default:
            IRepository<Skill> repo = ObjectFactory.GetInstance<IRepository<Skill>>();
            var s = new Skill { Name = name, Description = (string.IsNullOrEmpty(description)) ? string.Empty : description };
            repo.Save(s);
            return true;
    }
}

很奇怪,但是,如果我有这2个部分工作的话,我会最优化。 The how the ListBox is beingloaded in the view:

<%: Html.ListBox("Skills", Model.SkillList, new { @style = "width:157px; height:90px;background:#e2f0f1;", @size = "3", @class = "inputbox" })%>

起源于Registration ModelView.cs,载于我的模型:SricList:

private MultiSelectList GetSkills(string[] selectedValues)
{
    List<Skill> list = new List<Skill>();
    IRepository<Skill> skills = ObjectFactory.GetInstance<IRepository<Skill>>();

    foreach (Skill skill in skills.GetAll())
    {
        list.Add(new Skill()
        {
            Key = skill.Key,
            Name = skill.Name,
            Description = ""
        });                
    }
    return new MultiSelectList(list, "Key", "Name", selectedValues);
}

页: 1 意见

[MoveFormsScript]
public ActionResult Index()
{
    return View(new RegistrationModelView());
}

我完全相信我所贴出的所有代码(除了以WCF服务添加这一新项目,而花上述服务费的颜色无关紧要,但我认为我提供了尽可能多的信息)。

同我一样,数据库中增加了新的价值,没有问题,我的问题是更新名单Box,以反映新的价值。 没有人会提出想法,能够帮助这样做?

最佳回答

在我发现我需要做的事情之前,我uck忙。 这项任务可能不是最有效率或最有说服力的完成方式,但至少是行之有效的(也许有人会随同一种不同的解决办法。

我最后做的是,在第一次呼吁的succes上打另一个$,ajax()。

$("#AddNewSkill").click(function () {
    $("#AddNewSkill").attr("disabled", true);
    $("#newSkill").attr("disabled", true);
    var data = { name: $("#newSkill").val(), description: "", type: "skill" };
    data = JSON.stringify(data)
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "../WeddingPhotographerService.svc/AddNew",
        data: data,
        dataType: "json",
        success: function () {
            $.ajax({
                type:"POST",
                datatype:"json",
                url: "../Account/GetSkills",
                success:updateSkillsListBox
            });
        },
        error: function (msg) {
            alert(msg.d);
        }
    });
});

function updateSkillsListBox(data, status) {
    $("#Skills").html("");
    for(var d in data) {
        $("<option value="" + data[d].Value + "">" + data[d].Name + "</option>").appendTo("#Skills");
}
问题回答

暂无回答




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签