How to return back to a Kendo Window after a failed Submit?

1 Answer 71 Views
Editor Grid Window
Patrick
Top achievements
Rank 1
Patrick asked on 04 Jun 2024, 02:30 PM
I am using a kendo popup window as a form to edit items on a grid. If there are errors with defining the properties if should return to the window with error messages. This is Controller action. Currently if the modelstate is not valid it returns a view without and styling (just Html)> If I return View instead of Partial the page loads correctly but as a full page and not a window
public ActionResult EditActivity(EditActivity model)
		{
			var activity = _uow.Activities.GetById(model.ActivityId);

			activity.Phone = model.Phone;
			activity.Fax = model.Fax;
			activity.LastUpdateDate = DateTime.Now;
			activity.LastUpdatedBy = _sessionHandler.UserId;
			if (ModelState.IsValid)
			{
				try
				{
                    _uow.Activities.Update(activity);
                    _uow.Commit();
                    TempData["SaveResultMessage"] = $"Changes to Activity : {activity.ActivityName} have been saved";
                }
				catch
				{
                    ModelState.AddModelError("Save", "Error occurred on save.");
                }

				return RedirectToAction("Index", "DepartmentActivity", new
				{
					id = activity.DepartmentId,
				});
			}

			return View(model);

1 Answer, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 07 Jun 2024, 07:31 AM

Hi Patrick,

I noticed that there is no license associated with your account. Which inevitably limits our support service. I would strongly advocate going through our existing Support Plans in this regard:

Regardless, please allow me to at least give you some pointers regarding how the scenario can be handled. Alongside some additional explanations:

Normally, if you submit a form and stumble upon errors that are supplemented to the ModelState dictionary, it is mandatory to return the same view from which the model originated. This is the default convention to which the framework conforms.

There are indeed ways to circumvent this, however, it would require employing custom logic. For example via an AJAX request which after completion, populates the error messages within the form fields in the Window.

Due to my unfamiliarity with the fully established scenario on your side, I have assembled the following proof of concept example:

View:

@model TelerikMvcApp89.Models.FormModel

@(Html.Kendo().Window()
    .Name("window")
    .Content(@<text>
        @Html.Partial("_FormPartial", Model)
    </text>)
)

Partial View:

@model TelerikMvcApp89.Models.FormModel
    
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "my_form" }))
{
    @Html.TextBoxFor(m => m.Name)
    @(Html.ValidationMessageFor(m => m.Name))

    <button type="submit">Submit</button>
}

Controller:

[HttpPost]
public ActionResult Index(FormModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    return RedirectToAction("About");
}

This should then produce the following result:


Kind Regards,
Alexander
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.

Tags
Editor Grid Window
Asked by
Patrick
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Share this question
or