Search code examples
asp.net-mvcasp.net-mvc-4razor

Values not Submitting to controller action


i am working on view model SalesVM, Click on save button is to submitting the values to Create action... But when i click to save button, create form reloads and all fields empty.. (Weird for me) thing is that all valuse those i filled in textfields are now in url....!!!??

Under DOM tab in firebug,,, Error is: Predicted Url:sales/create?SMClientBranchId=1&IsCompleted=true&IsActive=true"

My view code is:

    @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's create actionspost is....

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

I dont know if its due to wrong submit method or validation problem,,,because form validation is also not working...any reference or help about debugging with firefox will also help... If some can, please help...Thanks for your time


Solution

  • i think you're using 2 <form> the first generated by razor @using (Html.BeginForm("Create", "Sales", FormMethod.Post, new { enctype = "multipart/form-data" })) and the second is the one you've written in this block! try to remove the second one!

     @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>
    }