Tanu
Tanu

Reputation: 125

Why the value is changed after clicking the submit button?

I make one action method for 2 activities (new input and edit), and there is also only one view to handle those activities. But I don't understand no matter what activity happen, the action method always think it is a new input. I learned that it because of the ID is always 0, but the problem is, when doing the edit, when in the view the ID is correct as the ID of the data, but when I click the submit button, the action method just see the 0 value of ID.

Here is the action method I used:

[HttpPost]
public ActionResult AddAssignment(SateliteSchedule SatSched)
{
    var txt = "";

    if (ModelState.IsValid)
    {
        if (SatSched.ID == 0)
        {
            db.SateliteSchedules.Add(SatSched);
            txt = "{0} has been added!";
        }
        else
        {
            db.Entry(SatSched).State = EntityState.Modified;
            txt = "{0} has been modified!";
        }

        db.SaveChanges();

        Utility utl = new Utility();
        TempData["message"] = string.Format(txt, utl.GetSateliteName(SatSched.SateliteID));

        return RedirectToAction("FormAssignment");
    }
    else
    {
        ViewBag.Message = "ModelState is not Valid!";

        return View("ErrorView");
    }
}

And here is the view:

@using (Html.BeginForm("AddAssignment", "admin", FormMethod.Post))
{
    @Html.ValidationSummary(true)

   <table>
     <tr>
       <td>@Html.LabelFor(m => m.Tanggal)
       </td>
       <td>
           @Html.EditorFor(m => m.Tanggal)
           @Html.ValidationMessageFor(m => m.Tanggal)
       </td>
     </tr>
     <tr>
       <td>@Html.LabelFor(m => m.SateliteID)</td>
       <td>
            @Html.DropDownList("SateliteID", (IEnumerable<SelectListItem>)ViewBag.SatList, "--- Satelite ---")
            @Html.ValidationMessageFor(m => m.SateliteID)
       </td>
     </tr>
     <tr>
       <td>@Html.LabelFor(m => m.WMOnDuty)</td>
       <td>
           @Html.DropDownList("WMOnDuty", (IEnumerable<SelectListItem>)ViewBag.WMList, "--- Worship Manager ---")
           @Html.ValidationMessageFor(m => m.WMOnDuty)
       </td>
     </tr>
     <tr>
       <td>@Html.LabelFor(m => m.SMOnDuty)</td>
       <td>@Html.EditorFor(m => m.SMOnDuty)</td>
     </tr>
     <tr>
       <td>@Html.LabelFor(m => m.WLOnDuty)</td>
       <td>@Html.EditorFor(m => m.WLOnDuty)</td>
     </tr>
     <tr>
       <td>@Html.LabelFor(m => m.MLOnDuty)</td>
       <td>@Html.EditorFor(m => m.MLOnDuty)</td>
     </tr>
     <tr>
       <td>@Html.LabelFor(m => m.SoundMan)</td>
       <td>@Html.EditorFor(m => m.SoundMan)</td>
     </tr>
     <tr>
       <td valign=top>@Html.LabelFor(m => m.Note)</td>
       <td>@Html.TextAreaFor(model => model.Note, new { @class = "memo-text" })</td>
     </tr>
   </table>
    <div>
       <input type="submit" value="Save" />
       @Html.ActionLink("Kembali", "FormAssignment")
    </div>
}

What should I check to fix this?

Upvotes: 0

Views: 166

Answers (1)

Dejan.S
Dejan.S

Reputation: 19138

You have to have the Id as a hidden, so when you go in the method the model will have the id asigned to it (does it make sense?). try placing this in your form

@Html.HiddenFor(m => m.ID)

Upvotes: 2

Related Questions