Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将变量值写入aspnetcore2中的mvc cshtml页面_C#_Razor_Asp.net Core Mvc - Fatal编程技术网

C# 如何将变量值写入aspnetcore2中的mvc cshtml页面

C# 如何将变量值写入aspnetcore2中的mvc cshtml页面,c#,razor,asp.net-core-mvc,C#,Razor,Asp.net Core Mvc,它是一个.aspnet core2站点。 我一直在用不同的方法进行“反复试验” 除了日期线,这些都没用,所以我知道razor在用。 我只想在代码中设置Userid的值,并将其显示为隐藏元素的值。没想到会变得这么复杂 Index.cshtml 签出控制器 使用系统; 使用System.Collections.Generic; 使用System.Threading.Tasks; 使用Microsoft.AspNetCore.Mvc; 使用Microsoft.AspNetCore.Authorizat

它是一个.aspnet core2站点。 我一直在用不同的方法进行“反复试验”

除了日期线,这些都没用,所以我知道razor在用。 我只想在代码中设置Userid的值,并将其显示为隐藏元素的值。没想到会变得这么复杂

Index.cshtml

签出控制器

使用系统;
使用System.Collections.Generic;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.Authorization;
使用NLog;
使用Microsoft.AspNetCore.Identity;
使用iBasiMobileV14;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Options;
使用iBasisMobileV14.模型;
使用iBasisMobileV14.Data.EntityModel;
使用iBasisMobileV14.class;
命名空间iBasisMobileV14.Controllers
{
公共类签出控制器:控制器
{
私有只读用户管理器_UserManager;
IHttpContextAccessor\U httpContextAccessor;
私有AppSettings_AppSettings{get;set;}
公共字符串用户标识{get;private set;}
公共签出控制器(
IHttpContextAccessor httpContextAccessor,
用户管理器用户管理器,
IOPS(应用程序设置)
{
_userManager=userManager;
_appSettings=appSettings.Value;
_httpContextAccessor=httpContextAccessor;
//会话需要设置某些内容,否则每次加载页面时都会发生更改
UserID=“ccccccc”;
}
公共IActionResult索引()
{
Guid userId=新Guid(_userManager.GetUserId(_httpContextAccessor.HttpContext.User));
ViewData[“UserID”]=“sdfdsfdfsdsfd”;//UserID;
//Logger.Info(“前”);
返回视图();
}
}
}

首先,您的视图被强类型化为
CheckoutViewModel
的实例,并且在视图中您正在访问UserID属性。但是在GET操作中,您没有向视图传递任何内容。因此,在您的视图中,基本上
Model
是空的。您不应该访问
NULL
上的属性/调用方法,但这正是您试图通过在视图中执行这一行
@Model.UserID
来实现的

因此,解决方案是,创建视图模型类的对象,并将该对象传递给视图

var vm=new CheckoutViewModel();
Guid userId = Guid.NewGuid();  // to do : Replace with your code to get a valid Guid
vm.UserID = userId.ToString();
return View(v);
在视图中,您可以使用输入标记帮助器

@model CheckoutViewModel
<input asp-for="UserID" type="hidden" />

您没有在GET方法中将模型传递给视图,因此该模型为
null
(您无法访问
null
对象的属性。只需使用
返回视图(您的模型)
传递
签出视图模型的实例(设置了
用户ID
);
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using NLog;
using Microsoft.AspNetCore.Identity;
using iBasisMobileV14;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using iBasisMobileV14.Models;
using iBasisMobileV14.Data.EntityModel;
using iBasisMobileV14.Classes;

namespace iBasisMobileV14.Controllers
{

    public class CheckoutController : Controller
    {
        private readonly UserManager<ApplicationUser> _userManager;
        IHttpContextAccessor _httpContextAccessor;
        private AppSettings _appSettings { get; set; }
        public string UserID { get; private set; }

        public CheckoutController(
           IHttpContextAccessor httpContextAccessor,
            UserManager<ApplicationUser> userManager,
              IOptions<AppSettings> appSettings)
        {
            _userManager = userManager;
            _appSettings = appSettings.Value;
            _httpContextAccessor = httpContextAccessor;
            //Session needs something set or it changes every page load

            UserID = "ccccccccc";


        }

        public IActionResult Index()
        {
            Guid userId = new Guid(_userManager.GetUserId(_httpContextAccessor.HttpContext.User));
            ViewData["UserID"] = "sdfdsfdfsdfdsfd"; //  userId;
            // Logger.Info("Front");
            return View();
        }
    }
}
var vm=new CheckoutViewModel();
Guid userId = Guid.NewGuid();  // to do : Replace with your code to get a valid Guid
vm.UserID = userId.ToString();
return View(v);
@model CheckoutViewModel
<input asp-for="UserID" type="hidden" />
public class CheckoutViewModel
{
  public string UserID { set;get;}
}