本文介绍了如何写编辑操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图想出一个编辑操作。请参阅下面什么,我有这么远。

视图模型:

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;命名空间GlobalUnitedSC.WebUI.Models
{
公共密封类CreateMensPlayerViewModel
{
    //球员资料从这里开始    [HiddenInput(DisplayValue = FALSE)]
    公众诠释MensTeamId {搞定;组; }    [HiddenInput(DisplayValue = FALSE)]
    公众诠释PlayerId {搞定;组; }    [需要]
    公共字符串名称{;组; }    [数据类型(DataType.Date)
    公众的DateTime?出生日期{搞定;组; }    [需要]
    公共字符串位置{搞定;组; }    公众诠释ShirtNumber {搞定;组; }    [数据类型(DataType.Date)
    公众的DateTime?加入{搞定;组; }    公共字符串国家{搞定;组; }    [数据类型(DataType.MultilineText)
    公共字符串描述{搞定;组; }    公共字节[] {的ImageData获得;组; }    [HiddenInput(DisplayValue = FALSE)]
    公共字符串ImageMimeType {搞定;组; }    [数据类型(DataType.EmailAddress)
    公共字符串EmailAddress的{搞定;组; }    [数据类型(DataType.PhoneNumber)
    公共字符串******中国{搞定;组; }    //玩家统计从这里开始
    公众诠释游戏{搞定;组; }
    公众诠释目标{搞定;组; }
    公众诠释助攻{搞定;组; }
    公众诠释TotalShots {搞定;组; }
    公众诠释ShotsOnGoal {搞定;组; }
    公众诠释FoulsDrawn {搞定;组; }
    公众诠释FoulsCommitted {搞定;组; }
    公众诠释保存{搞定;组; }
    公众诠释BlueCards {搞定;组; }
    公众诠释YellowCards {搞定;组; }
    公众诠释RedCards {搞定;组; }
 }
}

创建操作:

  [HTTPGET]
    公众的ActionResult创建(INT mensTeamId)
    {
        新CreateMensPlayerViewModel {MensTeamId = mensTeamId};        返回查看();
    }    [HttpPost]
    公众的ActionResult创建(CreateMensPlayerViewModel视图模型,HttpPostedFileBase图片)
    {
        如果(ModelState.IsValid)
        {
            VAR mensTeam = _dataSource.MensTeams.Single(T => t.Id == viewModel.MensTeamId);
            VAR mensPlayer =新MensPlayer
                                 {
                                     名称= viewModel.Name,
                                     出生日期= viewModel.BirthDate,
                                     位置= viewModel.Position,
                                     ShirtNumber = viewModel.ShirtNumber,
                                     加盟= viewModel.Joined,
                                     国家= viewModel.Country,
                                     说明= viewModel.Description,
                                     EmailAddress的= viewModel.EmailAddress,
                                     ******中国= viewModel.PhoneNumber,
                                     游戏= viewModel.Games,
                                     目标= viewModel.Goals,
                                     助攻= viewModel.Assists,
                                     TotalShots = viewModel.TotalShots,
                                     ShotsOnGoal = viewModel.ShotsOnGoal,
                                     FoulsDrawn = viewModel.FoulsDrawn,
                                     FoulsCommitted = viewModel.FoulsCommitted,
                                     节省= viewModel.Saves,
                                     BlueCards = viewModel.BlueCards,
                                     YellowCards = viewModel.YellowCards,
                                     RedCards = viewModel.RedCards
                                 };            mensTeam.MensPlayers.Add(mensPlayer);            _dataSource.Save();
            TempData的[消息] =的String.Format({0}已被保存,mensPlayer.Name);            返回RedirectToAction(细节,MensTeam,新{ID = viewModel.MensTeamId});
        }        返回视图(视图模型);
    }

HTTPGET编辑动作

  [HTTPGET]
    公众的ActionResult编辑(INT ID)
    {
        变种mensPlayer = _dataSource.MensPlayers.FirstOrDefault(p值=> p.Id == ID);        返回查看(mensPlayer);
    }

现在任何人都可以,请帮助我根据上述模型类的HttpPost Edit操作,preferably呢?

我希望它是与下面的线,如果这将创建一个新的球员,可我写的东西可以编辑播放器?

  VAR mensPlayer =新MensPlayer {}


解决方案

通过在转贴或这个问题延伸:评论Slauma的帮助:

Repost/Extension

这是什么,他建议我做的,它的工作原理。

添加到接口的IDataSource:

 无效更新(MensPlayer mensPlayer);

更新在DB类实现:

 无效IDataSource.Update(MensPlayer mensPlayer)
    {
        输入(mensPlayer).STATE = EntityState.Modified;
    }

编辑动作:

  [HttpPost]
    公众的ActionResult编辑(MensPlayer mensPlayer)
    {
        如果(ModelState.IsValid)
        {
            //保存播放器
            _dataSource.Update(mensPlayer);
           _dataSource.Save();           TempData的[消息] =的String.Format({0}已被保存,mensPlayer.Name);
            返回RedirectToAction(详细信息,MensPlayer,新{ID = mensPlayer.Id});
        }
        返回查看(mensPlayer);
    }

就这样一切工作正常,虽然我的假设下,我会实现更新整个DbSet像我保存做到了。

I am trying to come up with an edit action. See below for what i have so far.

ViewModel:

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; }
 }
}

Create Actions:

    [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 Edit Action

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

        return View(mensPlayer);
    }

Now could anyone please help me with the HttpPost Edit action, preferably one based on the model class mentioned above?

I was hoping it has something to do with the line below, if this creates a new player, what could i write to edit that player?

var mensPlayer = new MensPlayer {}
解决方案

With help of Slauma in the comments in the repost or extension of this question at:

Repost/Extension

This is what he suggested i do and it works.

Add to IDataSource Interface:

 void Update(MensPlayer mensPlayer);

Update Implemented in Db class:

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

Edit Action:

[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);
    }

And Just like that all works fine, although i was under the assumption that i would implement Update to the whole DbSet like i did with Save.

这篇关于如何写编辑操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:40