0

我正在尝试提出一个编辑操作。到目前为止,请参阅下文。

视图模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GlobalUnitedSC.WebUI.Models
{
public sealed class CreateMensPlayerViewModel
{
    //Player profile starts here

    [HiddenInput(DisplayValue=false)]
    public int MensTeamId { get; set; }

    [HiddenInput(DisplayValue = false)]
    public int PlayerId { get; set; }

    [Required]
    public string Name { get; set; }

    [DataType(DataType.Date)]
    public DateTime? BirthDate { get; set; }

    [Required]
    public string Position { get; set; }

    public int ShirtNumber { get; set; }

    [DataType(DataType.Date)]
    public DateTime? Joined { get; set; }

    public string Country { get; set; }

    [DataType(DataType.MultilineText)]
    public string Description { get; set; }

    public byte[] ImageData { get; set; }

    [HiddenInput(DisplayValue = false)]
    public string ImageMimeType { get; set; }

    [DataType(DataType.EmailAddress)]
    public string EmailAddress { get; set; }

    [DataType(DataType.PhoneNumber)]
    public string PhoneNumber { get; set; }

    //Player Statistics starts here
    public int Games { get; set; }
    public int Goals { get; set; }
    public int Assists { get; set; }
    public int TotalShots { get; set; }
    public int ShotsOnGoal { get; set; }
    public int FoulsDrawn { get; set; }
    public int FoulsCommitted { get; set; }
    public int Saves { get; set; }
    public int BlueCards { get; set; }
    public int YellowCards { get; set; }
    public int RedCards { get; set; }
 }
}

创建动作:

    [HttpGet]
    public ActionResult Create(int mensTeamId)
    {
        new CreateMensPlayerViewModel {MensTeamId = mensTeamId};

        return View();
    }

    [HttpPost]
    public ActionResult Create(CreateMensPlayerViewModel viewModel, HttpPostedFileBase  image)
    {
        if (ModelState.IsValid)
        {
            var mensTeam = _dataSource.MensTeams.Single(t => t.Id == viewModel.MensTeamId);
            var mensPlayer = new MensPlayer
                                 {
                                     Name = viewModel.Name,
                                     BirthDate = viewModel.BirthDate,
                                     Position = viewModel.Position,
                                     ShirtNumber = viewModel.ShirtNumber,
                                     Joined = viewModel.Joined,
                                     Country = viewModel.Country,
                                     Description = viewModel.Description,
                                     EmailAddress = viewModel.EmailAddress,
                                     PhoneNumber = viewModel.PhoneNumber,
                                     Games = viewModel.Games,
                                     Goals = viewModel.Goals,
                                     Assists = viewModel.Assists,
                                     TotalShots = viewModel.TotalShots,
                                     ShotsOnGoal = viewModel.ShotsOnGoal,
                                     FoulsDrawn = viewModel.FoulsDrawn,
                                     FoulsCommitted = viewModel.FoulsCommitted,
                                     Saves = viewModel.Saves,
                                     BlueCards = viewModel.BlueCards,
                                     YellowCards = viewModel.YellowCards,
                                     RedCards = viewModel.RedCards
                                 };

            mensTeam.MensPlayers.Add(mensPlayer);

            _dataSource.Save();
            TempData["message"] = string.Format("{0} has been saved", mensPlayer.Name);

            return RedirectToAction("detail", "MensTeam", new {id =         viewModel.MensTeamId});
        }

        return View(viewModel); 
    }

HttpGet 编辑动作

[HttpGet]
    public ActionResult Edit (int id)
    {
        var mensPlayer = _dataSource.MensPlayers.FirstOrDefault(p => p.Id == id);

        return View(mensPlayer);
    }

现在任何人都可以帮助我进行 HttpPost 编辑操作,最好是基于上述模型类的操作?

我希望它与下面的行有关,如果这会创建一个新播放器,我可以写什么来编辑该播放器?

var mensPlayer = new MensPlayer {}
4

2 回答 2

1

由于它是一个帖子,因此该方法有点等于您的创建方法。您将收到一个 MensPlayer 作为参数。

然后检查模型是否有效(验证等)并将条目标记为已修改并保存更改。

[HttpPost]
public ActionResult Edit(MyModel myModel)
{
    if (ModelState.IsValid)
    {
        DbContext.Entry(myModel).State = EntityState.Modified;
        DbContext.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(myModel);
}

数据库上下文

public class ModelContext : DbContext
{
    public DbSet<MyModel> MyModelSet{ get; set; }
}

有关 DBContext 的更多信息

于 2012-10-18T08:04:43.767 回答
0

在 Slauma 的帮助下,在此问题的转发或扩展中的评论中:

转发/扩展

这就是他建议我做的,并且有效。

添加到 IDataSource 接口:

 void Update(MensPlayer mensPlayer);

更新在 Db 类中实现:

void IDataSource.Update(MensPlayer mensPlayer)
    {
        Entry(mensPlayer).State = EntityState.Modified;
    }

编辑动作:

[HttpPost]
    public ActionResult Edit(MensPlayer mensPlayer)
    {
        if (ModelState.IsValid)
        {
            //Save Player
            _dataSource.Update(mensPlayer);
           _dataSource.Save();

           TempData["message"] = string.Format("{0} has been saved", mensPlayer.Name);
            return RedirectToAction("Detail", "MensPlayer", new {id = mensPlayer.Id});
        }
        return View(mensPlayer);
    }

就像这样,一切正常,尽管我假设我会像使用 Save 一样对整个 DbSet 实施更新。

于 2012-10-18T22:07:36.167 回答