forum

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

can't pass formCollection to create method in asp.net mvc

Anonymous User 2037 08-Dec-2014

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


Updated on 08-Dec-2014
I am a content writter !

Can you answer this question?


Answer

2 Answers

Liked By