如何将“提交按钮”值传递给控制器​​操作参数?

时间:2018-08-12 13:37:50

标签: c# html asp.net-core asp.net-core-mvc

我正在尝试使用ID为btnSaveAnContinue的按钮保存数据并编辑该数据。现在,每当我单击Save And Continue按钮保存数据时,post方法中的command参数即会获得Saveandcontinue,该参数已根据需要在按钮中定义。

但是每当我尝试编辑数据并单击相同的按钮将其保存时,AddEdit POST方法中的命令参数不会得到Saveandcontinue,而是null。 / p>

视图:

<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用于AddEdit:

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

POST用于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 个答案:

没有答案