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?

    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名