开发者

UpdateModel has no effect in my action controller after posting new values

开发者 https://www.devze.com 2023-03-04 13:55 出处:网络
Below is a portion of my controller: [Authorize] public ActionResult Edit(string IdAffaire) { Affaire affaire = this.repository.Retrieve(IdAffaire);

Below is a portion of my controller:

    [Authorize]
    public ActionResult Edit(string IdAffaire)
    {
        Affaire affaire = this.repository.Retrieve(IdAffaire);

        if (affaire == null)
        {
            return Redirect("~/");
        }

        var model = new AffaireEditViewModel
        {
            Affaire = a开发者_如何转开发ffaire,
            Status = repository.RetrieveStatus().Select(o => new SelectListItem { Text = o.Name, Value = o.IdStatus.ToString() }).ToList(),
        };

        return View(model);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(string idAffaire, AffaireEditViewModel model)
    {
        Affaire affaire = repository.Retrieve(idAffaire);

        if (!ModelState.IsValid)
        {
            return this.Edit(model.Affaire.IdAffaire);
        }

        try
        {
            UpdateModel(affaire);

            repository.Save();

            return RedirectToAction("Detail", "Affaire", new { idAffaire = idAffaire });
        }
        catch
        {
            return View(affaire);
        }

    }

Below is my ViewModel for edit:

public class AffaireEditViewModel
{
    public Affaire Affaire { get; set; }

    public IEnumerable<SelectListItem> Status { get; set; }
}

Below is my Affaire model:

public class Affaire
{
    [Key]
    public string IdAffaire { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Username { get; set; }
    public Int16? IdStatus { get; set; }
    public Int16? IdLabel { get; set; }
    // ....
}

My problem is that when posting new values in my edit view page, the action named Edit is well triggered (posting) with right values, but the statement UpdateModel(affaire) has no effect! Any help is greatly appreciated.

EDITED

I found the problem.

I need to change from this:

UpdateModel(affaire);

To this:

UpdateModel(affaire,"Affaire");

I guess it is because my view model is composed of several things and I need to tell explicitly to my UpdateModel function which element to use. Can somebody confirm?


To verify - is your repository retaining a reference to that instance of the model? I see you call save - but I don't see the implementation of the save since you aren't passing in a model to it.

0

精彩评论

暂无评论...
验证码 换一张
取 消