Question after click on submit button URL change How to keep it as it is without changes?

ahmedaziz

Well-known member
Joined
Feb 22, 2023
Messages
55
Programming Experience
1-3
I work on an ASP.NET MVC razor page. I face issue Url change from

Resignation/RequesterIndex?filenumber=103085

to

Resignation/Create

after click submit button.

I need URL as it was before, without change after click submit button. How to do that please?

What is issue on my code that make this issue?

My code details as below

full code details:
@model HR.WorkforceRequisition.Models.ResignationRequester

@{
    ViewBag.Title = "Requester Index";
}

@using (Html.BeginForm("Create", "Resignation", FormMethod.Post, new { enctype = "multipart/form-data", @id = "ResignationForm", style = "padding-top: 50px" }))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
    @if (!string.IsNullOrEmpty(ViewBag.errorMsg))
    {
        <div class="alert alert-danger">
            @ViewBag.errorMsg
        </div>
    }

    @if (!string.IsNullOrEmpty(ViewBag.successMessage))
    {
        <div class="alert alert-success">
            @ViewBag.successMessage
        </div>
    }

    <div class="row">
        <div class="form-group col-md-6 hover">
            <div class="col-md-5">
                @Html.LabelFor(model => model.Dept, htmlAttributes: new { @class = "control-label" })
            </div>

            <div class="col-md-7">
                @Html.EditorFor(model => model.Dept, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Dept, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group col-md-6 hover">
            <div class="col-md-5">
                @Html.LabelFor(model => model.Designation, htmlAttributes: new { @class = "control-label" })
            </div>

            <div class="col-md-7">
                @Html.EditorFor(model => model.Designation, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Designation, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>

    <div class="row">
        <div class="form-group col-md-6 hover">
            <div class="col-md-5">
                @Html.LabelFor(model => model.ResignationSubmissionDate, htmlAttributes: new { @class = "control-label" })
            </div>

            <div class="col-md-7">
                @Html.EditorFor(model => model.ResignationSubmissionDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ResignationSubmissionDate, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group col-md-6 hover">
            <div class="col-md-5">
                @Html.LabelFor(model => model.MobileNo, htmlAttributes: new { @class = "control-label" })
                <span class="text-danger"> *</span>
            </div>

            <div class="col-md-7">
                @Html.EditorFor(model => model.MobileNo, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.MobileNo, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-0 col-md-12">
            <input type="submit" value="Submit" class="btn btn-success" />
        </div>
    </div>
</div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}


public ActionResult RequesterIndex(string filenumber)
{
    return View(resignationRequester);
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(ResignationRequester resignationRequester)
{
    if (ModelState.IsValid)
    {
        try
        {
            Workforce.InsertToReignation(resignationRequester);
        }
        catch (Exception ex)
        {
            ViewBag.errorMsg = "Create not done correctly";
        }

        Session[SessionKeys.DirectManager] = GetEmployeeName(Convert.ToString(resignationRequester.DirectManager));
        Session[SessionKeys.LineManager] = GetEmployeeName(Convert.ToString(resignationRequester.LineManager));

        if (string.IsNullOrEmpty(ViewBag.errorMsg))
        {
            ViewBag.successMessage = "Resignation Submission form Created successfully";
        }
    }
    else
    {
        var errors = ModelState.Select(x => x.Value.Errors)
                               .Where(y => y.Count > 0)
                               .ToList();

        ViewBag.errorMsg = "Some required fields not added";
    }
}
          
return View(resignationRequester);
Model ResignationRequester:

public class ResignationRequester
{
    [Required]
    [Display(Name = "Dept./ Branch: ")]
    public string Dept { get; set; }

    [Required]
    [Display(Name = "Designation: ")]
    public string Designation { get; set; }

    [Required]
    [Display(Name = "Resignation Submission Date: ")]
    [DataType(DataType.Date, ErrorMessage = "Date only")]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime ResignationSubmissionDate { get; set; }

    [Required]
    [Display(Name = "Mobile No: ")]
    public string MobileNo { get; set; }
}
 
What is issue on my code that make this issue?

There is no issue. The code is working by design. HTML form posts work that way since the beginning of the HTTP forms since the days of Netscape. When the web browser posts to a form URL, that URL becomes the URL for the response. You are using HTML by your use of BeginForm().

If you don't like that behavior, either don't use form posts, or just make sure that you post to the same URL as your current page.
 

Latest posts

Back
Top Bottom