Home > DeveloperSection > Forums > can't pass formCollection to create method in asp.net mvc
Pooja Malohtra
Pooja Malohtra

Total Post:47

Points:331
Posted on    December-08-2014 2:12 AM

 ASP.Net C#  ASP.NET MVC 
Ratings:


 2 Reply(s)
 689  View(s)
Rate this:

I use asp.net 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...";

        }

 

        [HttpPost]

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

                }

                else

                {

                    break;

                }

 

            }

            return RedirectToAction("Index");

            //Addesslist.ForEach(p => context.SaveChanges());

            //if (context.SaveChanges() == Prod.Count)

            //{

            //    return RedirectToAction("Index");

            //}

            //else

            //{

            //    //Redirect to an error page or

            //    return View(brand);

            //}

        }

    }

View

 

model PMX_MVC.Models.Address

 

@{

    ViewBag.Title = "Index";

}

 

<h2>Index</h2>

 

@using (Html.BeginForm()) {

    @Html.AntiForgeryToken()

    @Html.ValidationSummary(true)

 

        <div class="table-responsive">

            <table id="address_table" class="table">

                <thead>

                    <tr>

                        <th>Street</th>

                        <th>City</th>

                        <th>Province</th>

                        <th>PostalCode</th>

                        <th>&nbsp;</th>

 

                    </tr>

                </thead>

                <tbody>

                    <tr>

                        <td>

                            <input id="Text1" type="text" name="street_01" maxlength="255" required class="street" /></td>

                        <td>

                            <input id="Text2" type="text" name="city_01" maxlength="255" required class="city" /></td>

                        <td>

                            <input id="Text3" type="text" name="province_01" maxlength="255" required class="province" /></td>

                        <td>

                            <input id="Text4" type="text" name="postalCode_01" maxlength="7" required class="postalCode" /></td>

                        <td>&nbsp;</td>

                    </tr>

                </tbody>

            </table>

        </div>

        <input type="button" value="Add Row" id="add_AdressRow" class="btn btn-lg btn-success btn-block" />

 

        <p>

            <input type="submit" value="Create" />

        </p>

 

}

 

<div>

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

</div>

 

@section Scripts {

    @Scripts.Render("~/bundles/jqueryval")

}



Tom Cruser
Tom Cruser

Total Post:28

Points:196
Posted on    December-08-2014 6:21 AM

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


Barbara Jones

Total Post:13

Points:91
Posted on    December-08-2014 7:36 AM

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

}

View

 

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


Don't want to miss updates? Please click the below button!

Follow MindStick