如何使用ajax.beginform将数据发送到控制器?

时间:2015-06-20 08:27:31

标签: ajax asp.net-mvc asp.net-mvc-4 razor

这是我在控制器" sale"

上的方法
[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Models.account account)
        {
            Models.sale creaventa = new Models.sale();
            //creaventa.account = cliente;
            creaventa.createdon = DateTime.Now;
            creaventa.idaccount = account.id;
            creaventa.modifiedon = DateTime.Now;
            creaventa.status = 0;

            context.sales.Add(creaventa);
            context.SaveChanges();

           // return "venta creada";
            return View();
        }

这是局部视图

@model List<modal3.Models.account>

@{
    ViewBag.Title = "Create";
}
<select class="form-control" id="control1">

    @{

        foreach (var cliente in Model)
        {
            <option value="@cliente.id"> @cliente.name</option>

        }



}
</select>

@*@using (Html.BeginForm("create", "sale", FormMethod.Post, new {id="my-form" }))
{
    @Html.AntiForgeryToken()
    <button type="submit" class="btn btn-default" value="Create" id="btncrear">
        Iniciar Venta
    </button>

}*@

@using (
    Ajax.BeginForm("create","sale",new AjaxOptions()
    {
        HttpMethod ="Post",
        InsertionMode = InsertionMode.Replace,


    })
    )
{
    @Html.AntiForgeryToken()
    <button type="submit" class="btn btn-default" value="Create" id="btncrear">
        Iniciar Venta
    </button>

}

输入方法时不会发送模型。

然后:

  • 如何发送模特?
  • 如何发送大量对象?

这是我的模特

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace modal3.Models
{
    using System;
    using System.Collections.Generic;

    public partial class sale
    {
        public sale()
        {
            this.saledetails = new HashSet<saledetail>();
        }

        public int id { get; set; }
        public Nullable<System.DateTime> createdon { get; set; }
        public Nullable<System.DateTime> modifiedon { get; set; }
        public Nullable<int> status { get; set; }
        public Nullable<int> idaccount { get; set; }

        public virtual account account { get; set; }
        public virtual ICollection<saledetail> saledetails { get; set; }
    }
}






using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;

    namespace modal3.Models
    {

        [MetadataType (typeof (sale_validation ))]
        public partial class sale
        {

        }

        public  class sale_validation
        {

            //2015-06-17 22:07:26.353   2015-06-17 22:07:26.353 1   1
            [Display (Name="")]
            [HiddenInput (DisplayValue =false )]
            public Nullable<System.DateTime> createdon { get; set; }
            [Display(Name = "")]
            [HiddenInput(DisplayValue = false)]
            public Nullable<System.DateTime> modifiedon { get; set; }
            [Display(Name = "")]
            [HiddenInput(DisplayValue = false)]
            public Nullable<int> status { get; set; }
            public Nullable<int> idaccount { get; set; }

        }
    }

1 个答案:

答案 0 :(得分:0)

为了让您了解AJAX FORM的工作原理,我创建了以下代码 -

让我们说我们的模型 -

public class Sale
{
    public string SaleOwner { get; set; }    
    public virtual Account Account { get; set; }
}

public class Account
{
    public string Name { get; set; }
}

我创建了two控制器操作 -

 public ActionResult adatas()
 {
     return View();
 }

 [HttpPost]
 public JsonResult Create(Sale s)
 {
      return Json("true");
 }

第一个控制器操作返回以下视图 -

@model WebApplication1.Controllers.Sale

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

@using (Ajax.BeginForm("Create", "Sale", new AjaxOptions()
{
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "done"
}))
{
    @Html.TextBoxFor(m => m.SaleOwner)
    @Html.TextBoxFor(m => m.Account.Name)

    <input type="submit" value="click" />
}

<div id="done">
</div>

查看渲染如下 -

enter image description here

点击按钮后,代码中有断点 -

enter image description here

一旦发生AJAX POST,输出将是 -

enter image description here