值未提交到控制器操作

时间:2015-04-13 06:27:03

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

我正在使用视图模型SalesVM,点击保存按钮就是将值提交给Create action ... 但是当我点击保存按钮时,创建表单重新加载并且所有字段都为空。 (对我来说很奇怪)事情就是那些填写文本字段的人现在都在网址中了.... !!! ??

在firebug中的DOM标签下,错误是:预测网址:销售/创建?SMClientBranchId = 1& IsCompleted = true& IsActive = true"

我的观看代码是:

    @model SM.CRM.AutosLoan.Models.Core.ViewModels.SalesVM

    @using (Html.BeginForm("Create", "Sales", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <form>
    @Html.TextBoxFor(model => model.RegistrationNumber)

    @Html.DropDownList("SMClientBranchId",IEnumerable<SelectListItem>)ViewBag.SMClientBranchId)

    @Html.CheckBoxFor(model => model.IsCompleted)

    @Html.CheckBoxFor(model => model.IsActive)

    <div class="form-group">
    <button type="submit" class="btn btn-sm btn-info" >Save</button>
    </div>
    </form>
}

Controller创建的actionspost是......

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "IsCompleted, SMClientBranchId, IsActive, CreatedBy, ModifiedBy, CreatedDate, ModifiedDate")] SalesVM salesvm)
{        
    if (ModelState.IsValid)
    {
        SalesDM salesdm = new SalesDM();
        // save sales info
        salesdm.SMClientBranchId = salesvm.SMClientBranchId;
        salesdm.IsActive = salesvm.IsActive;
        salesdm.IsCompleted = salesvm.IsCompleted;
        salesdm.CreatedBy = "someone";
        salesdm.CreatedDate = DateTime.Now;
        db.Sale.Add(salesdm);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View();
}

SaleVM:

 public class SalesVM
    {
        #region Public Properties
        public int SMClientBranchId { get; set; }
        public SMClientBranchesDM SMClientBranch { get; set; }
        public bool IsCompleted { get; set; }
        public bool IsActive { get; set; }
        public string CreatedBy { get; set; }
        public string ModifiedBy { get; set; }
        public DateTime? CreatedDate { get; set; }
        public DateTime? ModifiedDate { get; set; }

        #endregion
    }

我不知道它是否由于错误的提交方法或验证问题,,,因为表单验证也不起作用...任何有关使用firefox进行调试的参考或帮助也会有所帮助... 如果有人可以,请帮助...感谢您的时间

2 个答案:

答案 0 :(得分:2)

尝试在视图中将相关行更改为:

@using (Html.BeginForm("Create", "YourControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))

希望这会有所帮助......

答案 1 :(得分:2)

我认为你正在使用由剃刀<form>生成的第一个@using (Html.BeginForm("Create", "Sales", FormMethod.Post, new { enctype = "multipart/form-data" })),第二个是你在这个区块中编写的第一个!{1}}!尝试删除第二个!

 @model SM.CRM.AutosLoan.Models.Core.ViewModels.SalesVM

@using (Html.BeginForm("Create", "Sales", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.TextBoxFor(model => model.RegistrationNumber)

@Html.DropDownList("SMClientBranchId",IEnumerable<SelectListItem>)ViewBag.SMClientBranchId)

@Html.CheckBoxFor(model => model.IsCompleted)

@Html.CheckBoxFor(model => model.IsActive)
<div class="form-group">
<button type="submit" class="btn btn-sm btn-info" >Save</button>
</div>
}