简体   繁体   中英

No values comes into FormCollection while passing data from View to controller

I am trying to save the entity in edit mode. I want to pass the values from View to controller using FormCollection, but no value come into controller.

Here is markup:

 @using (Html.BeginForm("Edit", "Product", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmCreate" }))
{
    <div class =" row-fluid Span12" style ="margin-bottom :15px">
        <div class="span6" >
            <div class ="span4">
                <label>Product Name</label>
            </div>
            <div class ="span6">
                 <input type="text" value="@ViewBag.Name" id="ProductName" name="ProductName" style="height:20px; width:100%;" />
            </div>
        </div>
        <div class="span6" >
            <div class="span4" >
            <label > Product Code</label>
        </div>
        <div class="span6" >
            <input  type="text" value="@ViewBag.SectionCode" id="SectionCode"/> 
        </div>
    </div>
    <div class="span11" style="text-align:right">
        <input class="btn btn-primary" type="submit" value="Save" id="Edit"/>
        <button class="btn btn-default">Cancel</button>
    </div>
           }   

and this is my Controller Code

[HttpPost]
public ActionResult Edit(FormCollection form)
{
    Product product = new Product();
    product.SectionCode = Convert.ToString(form["SectionCode"]);
    product.Name = Convert.ToString(form["Name"]);

    return RedirectToAction("SaveData", oProductNew);
}

No value comes into FormCollection.

Probably, your view code does not belong to a form. Put it inside a form and try submitting it.

How about trying this. In your [HttpGet] method you do the following:

    [HttpGet]
    public ActionResult Edit()
    {
        Product product = new Product();
        return View(product);
    }

For [HttpPost] update your code to (this should work fine with binding):

    [HttpPost]
    public ActionResult Edit(Product product)
    {
        return RedirectToAction("SaveData", product);
    }

And lastly you can update your view to take the Product as view model:

    @model YourNamespace.Product @* Page View Model *@      
    @using (Html.BeginForm("Edit", "Product", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmCreate" }))
    {
        <div class =" row-fluid Span12" style ="margin-bottom :15px">
            <div class="span6" >
                <div class ="span4">
                    <label>Product Name</label>
                </div>
                <div class ="span6">
                    @Html.TextBoxFor(x => x.ProductName, new { @Style = "height:20px; width:100%;" }) @* TextBoxFor Extension Method *@
                </div>
            </div>
            <div class="span6" >
                <div class="span4" >
                <label > Product Code</label>
            </div>
            <div class="span6" >
                @Html.TextBoxFor(x => x.SectionCode)  @* TextBoxFor Extension Method *@          
            </div>
        </div>
        <div class="span11" style="text-align:right">
            <input class="btn btn-primary" type="submit" value="Save" id="Edit"/>
            <button class="btn btn-default">Cancel</button>
        </div>
    }   

The @Html extension methods will create all the names and id's that you need for data binding to occur automagically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM