m ali
m ali

Reputation: 646

modifying data in Database on Save method MVC

I have save function in MVC where the data gets stored to my database.

When i enter the form the save function adds the data... However to modify the current data i am struggling with.

Controller (Save)

         [HttpPost]
    public ActionResult Save(ReadingsUsersForm readingForm)
    {

        var model = new AddEditReadingsViewModel(readingForm);

        var addMeterReading = new M2CPDAL.Models.CustomerPortal.SerialReadingsUser();

                addMeterReading.ser_num = readingForm.SerialNumber;
                addMeterReading.prod_num = readingForm.ProductNumber;
                addMeterReading.UserName = readingForm.UserName;
                addMeterReading.AltUserName = readingForm.AltUserName;
                cpctx.SerialReadingsUsers.Add(addMeterReading);
                cpctx.SaveChanges();

                return View("_ReadingsUsers", model);
    }   

Model:

    public ReadingsUsersForm()
    {
        SerialNumber = ser_num;
        ProductNumber = _cpctx.vw_CurrentFleet.Where(s => s.ser_num == SerialNumber).Select(c => c.prod_num).FirstOrDefault();
        UserName = _cpctx.SerialReadingsUsers.Where(s => s.ser_num == ser_num).Select(c => c.UserName).FirstOrDefault();
        AltUserName = _cpctx.SerialReadingsUsers.Where(s => s.ser_num == ser_num).Select(c => c.AltUserName).FirstOrDefault();

    }

View

@using (Html.BeginForm("Save", "Device", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

<fieldset>
    <legend>Reading Details</legend>

    <div>
        @Html.LabelFor(m => m.SerialNumber)
        @Html.TextBoxFor(m => m.SerialNumber, new { @class = "form", @readonly = "readonly" })
        @Html.ValidationMessageFor(m => m.SerialNumber)
    </div>
    <div>
        @Html.LabelFor(m => m.ProductNumber)
        @Html.TextBoxFor(m => m.ProductNumber, new { @class = "form2", @readonly = "readonly" })
        @Html.ValidationMessageFor(m => m.ProductNumber)
    </div>

    <div>
        @Html.LabelFor(m => m.UserName)
        @Html.TextBoxFor(m => m.UserName, new { @id = "UserNameReading", @class = "form-add" })
        @Html.ValidationMessageFor(m => m.Form.UserName)
    </div>

    <div>
        @Html.LabelFor(m => m.AltUserName)
        @Html.TextBoxFor(m => m.AltUserName, new { @id = "AltUserNameReading", @class = "form-add2" })
        @Html.ValidationMessageFor(m => m.Form.AltUserName)

    </div>

    <div class="modal-footer">
        <input type="submit" id="ButtonSave" value="Submit" class="btn btn-primary">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
    </div>

</fieldset>

}

What i need is in the actionResult Save method to modify usernames. when i open the form and it has a username already in the database it will show this. but if i try changing this it will give me an error as there is already something in the database .

Thanks

Upvotes: 0

Views: 97

Answers (1)

Nikitesh
Nikitesh

Reputation: 1305

Modify your save method to fetch the user first and then try to save it:

     [HttpPost]
    public ActionResult Save(ReadingsUsersForm readingForm)
    {

        var model = new AddEditReadingsViewModel(readingForm);

        var addMeterReading = new M2CPDAL.Models.CustomerPortal.SerialReadingsUser();
        var existingUser = cpctx.SerialReadingsUsers.Where(x=>x.SerialNumber== readingForm.SerialNumber).FirstOrDefault();
       if(existingUser ==null)
       {
                addMeterReading.ser_num = readingForm.SerialNumber;
                addMeterReading.prod_num = readingForm.ProductNumber;
                addMeterReading.UserName = readingForm.UserName;
                addMeterReading.AltUserName = readingForm.AltUserName;
                cpctx.SerialReadingsUsers.Add(addMeterReading);
       }
       else
       {
                existingUser.ser_num = readingForm.SerialNumber;
                existingUser.prod_num = readingForm.ProductNumber;
                existingUser.UserName = readingForm.UserName;
                existingUser.AltUserName = readingForm.AltUserName;
        }
                cpctx.SaveChanges();

                return View("_ReadingsUsers", model);
    }  

This will update the existing entries as well as add new entries.

Upvotes: 1

Related Questions