1

I am trying to save data and edit that data using the button with Id btnSaveAnContinue. Now, whenever I click on Save And Continue button to save data, the command parameter in post method i.e gets Saveandcontinue which is defined in the button as per required.

But whenever, I try to edit the data and save it by clicking on same button, the command parameter in AddEdit POST method does not get Saveandcontinue, it is null.

The View:

<form asp-controller="Doctor" asp-action="AddEdit" method="post" class="form-horizontal" id="DoctorAddEdit" role="form">
    @Html.HiddenFor(c => c.DoctorId)
    <div class="m-b-md heading-tp-cls">
        <h3 class="m-b-none pull-left">Doctor Information <small id="currentUsersInCase"></small></h3>
        <div class="doc-buttons pull-right relative">
            <button type="submit" class="btn btn-s-md btn-success saveButton" id="btnSave"><i class="fa fa-save fa-fw"></i>Save And Close</button>
            <button type="submit" name="command" value="Saveandcontinue" id="btnSaveAnContinue" class="btn btn-s-md btn-success saveButton "><i class="fa fa-save fa-fw"></i>Save And Continue</button>
        </div>
        <div class="clearfix"></div>
    </div>
    <section class="panel panel-default tp-section-cls no-left-right-borders" style="padding-top: 10px;">
        <div class="row m-l-none m-r-none bg-light lter">
            <section>
                <div class="col-lg-2 col-md-2 col-sm-6 error-holder-form hideSubject" id="claimanatFirstNameDiv">
                    <label>First Name<span class="requAstrik">*</span></label>
                    <input asp-for="FirstName" id="DFirstName" class="form-control subjectHide" placeholder="" type="text">
                    <span asp-validation-for="FirstName" class="text-danger error"></span>
                </div>
                <div class="col-lg-2 col-md-2 col-sm-6 hideSubject" id="claimanatMiddleInitialDiv">
                    <label>Middle Name</label>
                    <input asp-for="MiddleInitial" id="DMidName" class="form-control subjectHide" placeholder="" type="text">
                </div>
            </section>
        </div>
    </section>
</form>

GET method for AddEdit:

public IActionResult AddEdit(int id = 0, bool flag = false)
{
    var getDoctorById = _doctorService.GetDoctorInfoById(id);
    return View(getDoctorById);
}

POST method for AddEdit:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddEdit(DoctorInfoModel doctorInfo, string command)
{
    bool isAjaxCall = HttpContext.Request.Headers["x-requested-with"] == "XMLHttpRequest";
    if (isAjaxCall)
    {
        return Content("Saved");
    }
    if (ModelState.IsValid)
    {
        var result = 0;
        int userId = User.GetUserId();
        doctorInfo.CreatedBy = userId;
        var saveAndGetUserId = 0;
        if (doctorInfo.DoctorId == 0)
        {
            saveAndGetUserId = _doctorService.SaveUserInfo(doctorInfo);
            var user = new ApplicationUser { UserName = doctorInfo.UserName, Email = doctorInfo.DocEmail, UserId = saveAndGetUserId };
            var getResult = await _userManager.CreateAsync(user, doctorInfo.Password);
            _doctorService.SaveUserRole(user.Id);
        }
        else
        {
            var getUserId = _doctorService.GetUser(doctorInfo);
            var user = _userManager.Users.FirstOrDefault(c => c.UserId == getUserId);
            user.UserName = doctorInfo.UserName;
            user.Email = doctorInfo.DocEmail;
            if (doctorInfo.Password != null && !"Password@123".Equals(doctorInfo.Password))
            {
                user.PasswordHash = _userManager.PasswordHasher.HashPassword(user, doctorInfo.Password);
            }
            try
            {
                var result1 = await _userManager.UpdateAsync(user);
            }
            catch (Exception ex)
            {

                throw;
            }
        }
        var getSaveResult = _doctorService.SaveDoctor(doctorInfo, saveAndGetUserId);

        if (getSaveResult.Id > 0)
        {
            Success(getSaveResult.MessageBody);
        }
        else
        {
            Warning(getSaveResult.MessageBody);
        }
        if (command == "Saveandcontinue")
        {
            return RedirectToAction(nameof(DoctorController.AddEdit), new { id = getSaveResult.Id, flag = true });
        }
        else
        {
            return RedirectToAction(nameof(HomeController.Index), "Doctor");
        }
    }

    Warning("Failed to save Doctor, try again later.");
    return View("AddEdit", doctorInfo);
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.