.net mvc and editing data with forms

I have a form for editing concerts.

The initial controller action is called "Edit".

The form is submitted to a second controller action called "Refresh"

So, once the form is submitted, I use a custom ModelBinder that uses bindingContext.ModelState.AddModelError to add validation messages to the modelstate

The Update copntroller action looks like this:

[AcceptVerbs("POST")]
    public ActionResult Update(Guid id, FormCollection formCollection)
    {
        Gig gig = GigManager.GetByID(id);
        try
        {
            UpdateModel<Gig>(gig);
            GigManager.Save(gig);
            return RedirectToAction("List");

        }
        catch (Exception e)
        {
           return View(gig);    
        }

    }

      

If the model block has errors, the exception will be thrown by the update model.

This means that RedirectToAction is called so that the original controller action, Edit, is called.

This means that I will not see my validation messages and any data added by the user to the form will be reset to its original values!

How do I approach this?

I have included the "modify" action below:

[AcceptVerbs("GET")]
    public ActionResult Edit(Guid id)
    {
        Gig gig = GigManager.GetByID(id);

        SelectList days = CreateDays(1, 31, 1,  gig.StartDate.Day);
        ViewData["day"] = days;

        SelectList months = CreateMonths(1, 12, 1, gig.StartDate.Month);
        ViewData["month"] = months;

        SelectList years = CreateYears(DateTime.Now.Year, DateTime.Now.Year + 10, 1, gig.StartDate.Year);
        ViewData["year"] = years;

        string bandNames ="";
        string bandIds = "";
        foreach(Act act in  gig.Acts)
        {
            bandNames += act.Name.Trim() + ", ";
            if (act.Artist != null)
            {
                bandIds += act.Artist.ID + ";";
            }
        }

        ViewData["Bands"] = bandNames;
        ViewData["BandIds"] = bandIds;

        return View(gig);

    }

      

However, I am not getting th verification messages

+1


source to share


1 answer


Maybe this will help. I just assigned a controller that does write / edit admin. It uses a binding for a class, which can be handy. Look at the very end of the file to see a possible way to handle the Get and Post verbs. Note that UpdateModelStateWithViolations is just a helper for adding errors to ModelState.

        Controller.ModelState.AddModelError(violation.PropertyName,
            violation.ErrorMessage);

      

which is displayed with

<%= Html.ValidationSummary() %>

      



http://www.codeplex.com/unifico/SourceControl/changeset/view/1629#44699

and view: http://www.codeplex.com/unifico/SourceControl/changeset/view/1629#54418

    [AcceptVerbs("GET")]
    [Authorize(Roles = "Admin")]
    public ActionResult EditRole(Guid? RoleID)
    {
        Role role = null;
        RoleForm form = new RoleForm { };
        if (RoleID.HasValue)
        {
            role = accountService.GetRole(RoleID.Value);
            if (role == null)
                return RedirectToAction("Roles");

            form = new RoleForm
            {
                RoleID = role.ID,
                RoleName = role.Name,
                Level = role.Level
            };
        }
        else
        {
            form = new RoleForm();
        }

        ViewData.Model = form;

        return this.PluginView();
    }


    [AcceptVerbs("POST")]
    [Authorize(Roles = "Admin")]
    public ActionResult EditRole(Guid? RoleID, [Bind(Include = "RoleID,RoleName,Level", Prefix = "")] RoleForm form)
    {
        Role role = null;
        if (RoleID.HasValue)
        {
            role = accountService.GetRole(RoleID.Value);
            if (role == null)
                return RedirectToAction("Roles");
        }

        ServiceResponse<Role> response = accountService.AttemptEdit(form);

        if (response.Successful)
        {
            TempData["Message"] = "Update Successfull";
        }
        else
        {
            this.UpdateModelStateWithViolations(response.RuleViolations);
        }

        //ViewData["AllRoles"] = accountService.GetRolePage(new PageRequest(0, 50, "Name", typeof(string), true)).Page.ToArray();


        ViewData.Model = form;

        return this.PluginView();
    }

      

+1


source







All Articles