can't pass formCollection to create method in mvc

Total Post:47

 1443  View(s)
Rate this:

I use MVC I and create a HTML Form not razor I tried to used form collection to pass data from form to View to controller public ActionResult Create(Address address) but no use can someone help me ? 

Address controller

  public class AddressController : Controller
        // GET: /Address/
        public ActionResult Index()
            return View();
        // GET: /Address/Welcome/
        public string Welcome()
            return "This is the Welcome action method...";
        public ActionResult Create(Address address)
            //Loop through the request.forms
            var Addesslist = new List<Address>();
            for (int i = 0; i <= Request.Form.Count; i++)
                var street = Request.Form["street_0" + i + ""];
                var city = Request.Form["city_0" + i + ""];
                var postalCode = Request.Form["postalCode_0" + i + ""];
                var province = Request.Form["province_0" + i + ""];
                var personID = 1;
                if (street != null && city != null && postalCode != null && province != null)
                    Addesslist.Add(new Address { Street = street, City = city, Province = province,PostalCode = postalCode,PersonID = personID});
            return RedirectToAction("Index");
            //Addesslist.ForEach(p => context.SaveChanges());
            //if (context.SaveChanges() == Prod.Count)
            //    return RedirectToAction("Index");
            //    //Redirect to an error page or
            //    return View(brand);


model PMX_MVC.Models.Address 
    ViewBag.Title = "Index";

@using (Html.BeginForm()) {
        <div class="table-responsive">
            <table id="address_table" class="table">
                            <input id="Text1" type="text" name="street_01" maxlength="255" required class="street" /></td>
                            <input id="Text2" type="text" name="city_01" maxlength="255" required class="city" /></td>
                            <input id="Text3" type="text" name="province_01" maxlength="255" required class="province" /></td>
                            <input id="Text4" type="text" name="postalCode_01" maxlength="7" required class="postalCode" /></td>
        <input type="button" value="Add Row" id="add_AdressRow" class="btn btn-lg btn-success btn-block" /> 
            <input type="submit" value="Create" />

    @Html.ActionLink("Back to List", "Index")
@section Scripts {

  1. Post:28

    Re: can't pass formCollection to create method in mvc

    Your actual issue is that this: 

    @using (Html.BeginForm())

    ..will post to a POST action method called Index. You need to specify the method you want to post to if this isn't what you want: 

    @using (Html.BeginForm("Create", "Address", FormMethod.Post))

      Modified On Apr-07-2018 04:18:29 AM
  1. Post:13

    Re: can't pass formCollection to create method in mvc

    Any reasons why you are using Request.Form instead of the Address parameter in your action method? 

    Use of the Model Binder will be easier. You just have to name your input tags to match your Address model. 

    Address model: 

    public class Address
       public string Street {get;set;}
       public string Province {get;set;}
       public string PostalCode {get;set;}


    <input id="Text1" type="text"name="Street" maxlength="255" required class="street"

      Modified On Apr-07-2018 04:14:13 AM