csdn产品小助手 2016-06-01 18:10 采纳率: 0%
浏览 31

使用ajax更新列表。

I am currently trying to update a list of duties after the user has posted a new duty. I am trying to use Ajax doing this. However I am not shure how to use ajax for posting to my controller and then add to my list. How do I use my datafield in my ajax method and how do I update my list?

My controller:

        [HttpPost]
        Public ActionResult CreateDuty(PartialViewModel d)
{
        db.DutiesTable.Add(d.OneDuty);
                    db.SaveChanges();
                    pv.DutyList = db.DutiesTable.ToList();

                    return View("Index");
    }

My Ajax method:

<script>
    $('#getDuty').click(function () {
        $.ajax({
            type: 'POST',
            url: "/Home/CreateDuty",
            data: {d : d}
        }).success(function (data) {
            alert(data + status);
        });
    });
</script>
  • 写回答

1条回答 默认 最新

  • weixin_33682790 2016-06-01 18:33
    关注

    Not enough information. Where did d come from and what is it (javascript).

    For your EF code I assume DutiesTable does NOT contain PartialViewModel's so there is a mapping that needs to happen there between the view model and EF model classes.

    [HttpPost]
    Public ActionResult CreateDuty(DutyViewModel d)
    {
        Duty newduty = new Duty(){ /*map values from d to fields here*/}
        db.Duties.Add(newduty);
        db.SaveChanges();
        return View("Index");
    }
    

    Once you have your data posting over the controller and saving with EF you need to decide on if your javascript needs a copy of the updated entity (mapped back to a PartialViewModel most likely or if a simple success/fail message is enough and do what it needs to on that side. I don't know what you mean here: "and then add to my list" - what list?

    评论

报告相同问题?