EscapeDataString不能为空

时间:2018-05-23 08:14:35

标签: c# model-view-controller

我正在使用MVC,我正在尝试从mvc创建一个帐户,但是我希望它通过仅在创建视图中提供他们的steamid来将用户的用户名设置为他们的Steam昵称。

public string GetUsernameFromId(string Steamid)
{
    string steamid2 = Steamid.ToString();
    XDocument doc = XDocument.Load("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=steamapihide&steamids=" + Uri.EscapeDataString(steamid2) + "&format=xml");

    string UserNames = (string)doc.Descendants("personaname").FirstOrDefault();

    return UserNames;
}

在控制器中我有这个

  [HttpGet]
        public ActionResult Create(string Tradelink, string password, string Steamid)
    {
        Balance accBalance = bctrl.CreateonCreateAccount();

        SteamAccount acc = new SteamAccount
        {
            SteamId = Steamid,
            UserName = GetUsernameFromId(Steamid),
            Password = password,
            accountbalance = accBalance,
            TradeLink = Tradelink

        };
        Create(acc);
        return View();
    }

    [HttpPost]
    public ActionResult Create(SteamAccount acc)
    {
        if (ModelState.IsValid)
        {
            Balance accountbalance = bctrl.CreateonCreateAccount();
            acc.accountbalance = accountbalance;
            acc.UserName = GetUsernameFromId(acc.SteamId);
            foreach (var item in acc.AccountSkins)
            {
                sctrl.CreateonCreateAccount(item);
            }
            AccountRepo.Insert(acc);
            AccountRepo.Save();
            return RedirectToAction("Index");
        }
        else
        {
            return View(acc);
        }
    }

这是我的模特课

namespace CSGO_MVC.Models

{
    public class SteamAccount
    {
        [Key]
    public int Id { get; set; }

    public string SteamId { get; set; }

    public Balance accountbalance { get; set; }

    [Required(ErrorMessage = "Please Provide Username", AllowEmptyStrings = false)]
    public string UserName { get; set; }

    [Required(ErrorMessage = "Please provide password", AllowEmptyStrings = false)]
    [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
    public string Password { get; set; }

    public bool UserStatus { get; set; }

    public string TradeLink { get; set; }

    public List<Skin> AccountSkins { get; set; }

所以我得到的错误是当我尝试运行时,Steamid在GetUsernameFromId方法中不能为空。

任何帮助?

0 个答案:

没有答案