[RESOLVED]Very confusion in Edit Method

I have Edit method for GET and POST as follows : 

This is my GET method , pls note that i have passed two parameters here 

 public ActionResult Edit(int ItemNo, double? ApprovedPeriod)
        {
            
            int ContractID = 555;
            int CertificateId = 25;

            var cms_category_roadmaintenance = from m in db.CMS_Category_RoadMaintenance.Where(x => x.Contract_Id == ContractID && 
x.Certificate_Id == CertificateId && x.Item_No == ItemNo && x.Approved_Period == ApprovedPeriod && x.Delete_Flag == "N") select m ; IEnumerable<RoadMaintenenceEdit> roadmaintenenceedit = new List<RoadMaintenenceEdit>(); roadmaintenenceedit = cms_category_roadmaintenance.Select(x => new RoadMaintenenceEdit() { RoadMaintenance_Id = x.RoadMaintenance_Id, Length = x.Length, Period = x.Period, Contract_SDate_G = x.Contract_SDate_G, Contract_EDate_G = x.Contract_EDate_G, IsIncludePrevCert = x.IsIncludePrevCert, TotalPrice = x.TotalPrice }); return View(roadmaintenenceedit.ToList()); }

This is my POST method, pls note i have passed an object as a parameter here

 [HttpPost]
 [ValidateAntiForgeryToken]
 public ActionResult Edit([Bind(Include ="RoadMaintenance_Id,Item_No,Contract_SDate_G,Contract_EDate_G,Period,Length,IsIncludePrevCert,
TotalPrice")] RoadMaintenenceEdit roagmaintenanceedit) { if (ModelState.IsValid) { Do something } return View(); }

Am passing the object parameter from the edit view to the POST method, but it always hitting the edit POST method with the Edit GET parameters

int ItemNo, double? ApprovedPeriod

The object parameter value is showing as null.. How to solve this problem ???

Below is my Edit View

@model  IEnumerable<RMCMS_MVC_DEMO1.UI.Models.ViewModel.RoadMaintenenceEdit>

@{
    ViewBag.Title = "Road Maintenance Details";
}
<div id="ContractDetails"></div>

@foreach (var item in Model)
{
    using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
            @Html.ValidationSummary(true)
            <div class="form-group">
                @Html.LabelFor(modelItem => item.RoadMaintenance_Id, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => item.RoadMaintenance_Id)
                    @Html.ValidationMessageFor(model => item.RoadMaintenance_Id)
                </div>
            </div>
            @*  *@
            <div class="form-group">
                @Html.LabelFor(model => item.Length, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => item.Length)
                    @Html.ValidationMessageFor(model => item.Length)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => item.Contract_SDate_G, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => item.Contract_SDate_G)
                    @Html.ValidationMessageFor(model => item.Contract_SDate_G)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => item.Contract_EDate_G, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => item.Contract_EDate_G)
                    @Html.ValidationMessageFor(model => item.Contract_EDate_G)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => item.Period, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => item.Period)
                    @Html.ValidationMessageFor(model => item.Period)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => item.IsIncludePrevCert, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => item.IsIncludePrevCert)
                    @Html.ValidationMessageFor(model => item.IsIncludePrevCert)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => item.TotalPrice, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => item.TotalPrice)
                    @Html.ValidationMessageFor(model => item.TotalPrice)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Save" class="btn btn-default" />
                </div>
            </div>
        </div>
    }

}

instead of

@Html.EditorFor(model => item.RoadMaintenance_Id)


put an
<input type=text name='RoadMaintenance_Id' value='@item.RoadMaintenance_Id' />

@ignatandrei  ,

Thanks very much for the kind help. Its working.. But whats the secret behind that ?? Why its not working for @Html.EditorFor() ??

Read 

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

Thanks for guidence

Hey ignatandrei,

           Your solution was working. But i have another problem. if i use <input type=textbox> instead of @Html.EditorFor(), the validations which i declared at model using data annotations will not work. how to solve this. 

mdsadullah718

if i use <input type=textbox> instead of @Html.EditorFor(), the validations which i declared at model using data annotations will not work. how to solve this. 

copy manual the validations tag from @Html.EditorFor() into <input type=textbox>

try this

<input type=text name='RoadMaintenance_Id' value='@item.RoadMaintenance_Id' 
data-val="true" data-val-required="The RoadMaintenance field is required." id="RoadMaintenance_Id"
/>
<span class="field-validation-valid" data-valmsg-for="RoadMaintenance_Id" data-valmsg-replace="true"></span>

@ignatandrei and @cnuonline,

 Thanks for your kind help. Its working. Can you please tell me is there any way to use @Html.EditorFor()  instead of <input type=text> , because all my views are created using T4 template. There are hundreds of views. Pls guide me !!

I have found the villain of the problem . Its the name attribute, if i change the Name attribute every thing works well. But how to change the Name attribute in @Html.EditorFor() . Even i tried in  @Html.TextBoxFor() but name not changing. Any ideas ??

Leave a Reply