I work on asp.net MVC razor page . I face issue Url change from
Resignation/RequesterIndex?filenumber=103085
to
Resignation/Create
after click submit button
so I need URL still as it is before without change after click submit button .
I need after click submit button URL not change so How to do that please ?
What is issue on my code that make this issue .
my code details as below
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; }
@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()
@if (!string.IsNullOrEmpty(ViewBag.errorMsg))
{
@ViewBag.errorMsg
}
@if (!string.IsNullOrEmpty(ViewBag.successMessage))
{
@ViewBag.successMessage
}
@Html.LabelFor(model => model.Dept, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Dept, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Dept, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Designation, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Designation, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Designation, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.ResignationSubmissionDate, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.ResignationSubmissionDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ResignationSubmissionDate, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.MobileNo, htmlAttributes: new { @class = "control-label" })
*
@Html.EditorFor(model => model.MobileNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MobileNo, "", new { @class = "text-danger" })
}
[ValidateAntiForgeryToken]
public async Task 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);
}
Amit MohantyPosted Sep 11, 2023, 5:37 AM
The issue you're facing where the URL changes after clicking the submit button is because of the default behavior of HTML forms in ASP.NET MVC. When you submit a form, it typically performs a POST request to the specified action method, which results in a URL change to the URL associated with that action method. In your case, you are posting the form to the "Create" action method in the "Resignation" controller, which is why the URL changes.
If you want to keep the URL unchanged after submitting the form, you can consider using AJAX to submit the form asynchronously. This way, you can send the form data to the server without causing a full page reload, and the URL will remain the same.
Remove the
Html.BeginFormblock from your Razor view. Instead, add an HTML form with an ID to identify it for JavaScript handling:Add JavaScript code to handle the form submission via AJAX: