Model Binding returning Guid as not valid

Viewed 36

Hi I am currently using guids as my ID for a MVC project I am working on. The database creates the guid on a create however when I try and edit a record I get the following returned by the model state.

The value 'd248eb16-48f8-4f29-1afa-08da9abfd705' is not valid.

I have tried to insert a custom validation to try an intercept the error however the model binding is creating the error.

I have run this through a guid checker and it is a valid guid, before I go back and use longs for the Id's any idea on why the model binding is saying this is not valid.

I have a base with my common fields

    public abstract class BaseModel : IBaseModel
{
    public Guid? Id { get; set;}
    public string? OwnerId { get; set;}
    public bool Deleted { get; set;}
}

    public class Club : BaseModel, IClub
{
    public string Name { get; set;}

    public string Instructor {get;set;}
}

As my edit and create are identical I have created a partial view

    @using Masc_Model.View_Models
    @model ClubVM

    @Html.HiddenFor(m=>m.Id)
     <div asp-validation-summary="ModelOnly" class="text-danger"></div>

     <div class="row">

         <div class="col-md-1">   <label asp-for="Name" class="control- 
     label"></label> </div>
          <div class="col-md-3">    <input asp-for="Name" class="form- 
    control" /> </div>
         <div class="col-md-3">     <span asp-validation-for="Name" 
     class="text-danger"></span>    </div>
      </div>
        <p></p>
 <div class="row">

         <div class="col-md-1">   <label asp-for="Instructor" 
    class="control-label"></label> </div>
          <div class="col-md-3">    <input asp-for="Instructor" 
    class="form-control" /> </div>
         <div class="col-md-3">     <span asp-validation- 
   for="Instructor" class="text-danger"></span>    </div>
     </div>

    <p></p>
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn- 
   primary" />
            </div>

which is used in the edit and create views

    @using Masc_Model.View_Models
    @model ClubVM

    @{
        ViewData["Title"] = "Edit";
     }

    <h3>Edit Club</h3>
    <p></p>
    <form asp-action="Edit">
         @Html.Partial("..\\Club\\_edit",Model)
    </form>

my edit method for the controller

     // POST: ClubController/Edit/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Guid id, [FromForm] Club club)
    {
        try
        {
            if (ModelState.IsValid)
            {
                data.UpdateItem(club);
                return RedirectToAction("Details", new { id = club.Id });
            }

            var clubvm = data.GetViewModel(club);
            return View(clubvm);
        }
        catch (DbUpdateConcurrencyException ex)
        {
            ModelState.AddModelError("", DbConncurancyError);

            var latestValues = data.GetItem(club.Id);

            return View(latestValues);
        }
        catch
        {
            var clubvm = data.GetViewModel(club);
            return View(clubvm);
        }
    }
0 Answers
Related