2015-06-20 51 views
0

这是我的控制器“买卖”如何用ajax.beginform发送数据给控制器?

[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> 

} 

这并进入到方法与此不发送模式。

然后:

  • 如何发送模式?
  • 如何发送很多对象?

    比方说,我们的模型 - -

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

    我创建two控制器

这是我的模型

//------------------------------------------------------------------------------ 
// <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; } 

     } 
    } 
+0

在你的'AJAX FORM'我没有看到任何控件(需要被发布到服务器),那么什么是你想发布?你可以删除注释代码并设置你的代码的格式,并告诉我们你在代码中要做什么,这样我就可以为你做一个样本。 – ramiramilu

+0

我不知道,但我相信你没有看到它@using( Ajax.BeginForm( “创造”, “销售”,新AjaxOptions(){ 列举HTTPMethod = “邮报”, InsertionMode = InsertionMode.Replace, }) ) { @ Html.AntiForgeryToken() <按钮类型= “提交” 类= “BTN BTN-默认” 值= “创建” ID = “btncrear”> Iniciar文塔 } – angel

+0

我看到了这段代码,但在'AJAX FORM'中没有控件,所以你要发布什么? – ramiramilu

回答

0

为了让您了解如何AJAX FORM的作品,我下面的代码创建行动 -

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

相关问题