Asp.net 通过ViewBag传递用户ID

Asp.net 通过ViewBag传递用户ID,asp.net,asp.net-mvc,asp.net-mvc-5,asp.net-identity,Asp.net,Asp.net Mvc,Asp.net Mvc 5,Asp.net Identity,我试图在创建操作中保存标识UserID 控制器GET请求如下所示: // GET: Owners/Create public ActionResult Create() { ViewBag.RegUser = User.Identity.GetUserId(); return View(); } @Html.HiddenFor(model => model.RegUserID, new { @value = ViewBag.RegUser }) // POST: Own

我试图在创建操作中保存标识UserID

控制器GET请求如下所示:

// GET: Owners/Create
public ActionResult Create()
{
    ViewBag.RegUser = User.Identity.GetUserId();
    return View();
}
@Html.HiddenFor(model => model.RegUserID, new { @value = ViewBag.RegUser })
// POST: Owners/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "OwnerID,OwnerName,ContactName,PhysicalAddress1,PhysicalAddress2,PhysicalCity,PhysicalState,PhysicalCountry,PhysicalPostCode,PostalAddress1,PostalAddress2,PostalCity,PostalState,PostalCountry,PostalPostCode,Phone,Mobile,Fax,Email,RegUserID")] Owner owner)
{
    if (ModelState.IsValid)
    {
        db.Owners.Add(owner);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(owner);
}
@Html.HiddenFor(model => model.RegUserID)
意见如下:

// GET: Owners/Create
public ActionResult Create()
{
    ViewBag.RegUser = User.Identity.GetUserId();
    return View();
}
@Html.HiddenFor(model => model.RegUserID, new { @value = ViewBag.RegUser })
// POST: Owners/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "OwnerID,OwnerName,ContactName,PhysicalAddress1,PhysicalAddress2,PhysicalCity,PhysicalState,PhysicalCountry,PhysicalPostCode,PostalAddress1,PostalAddress2,PostalCity,PostalState,PostalCountry,PostalPostCode,Phone,Mobile,Fax,Email,RegUserID")] Owner owner)
{
    if (ModelState.IsValid)
    {
        db.Owners.Add(owner);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(owner);
}
@Html.HiddenFor(model => model.RegUserID)
控制员职位申请如下:

// GET: Owners/Create
public ActionResult Create()
{
    ViewBag.RegUser = User.Identity.GetUserId();
    return View();
}
@Html.HiddenFor(model => model.RegUserID, new { @value = ViewBag.RegUser })
// POST: Owners/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "OwnerID,OwnerName,ContactName,PhysicalAddress1,PhysicalAddress2,PhysicalCity,PhysicalState,PhysicalCountry,PhysicalPostCode,PostalAddress1,PostalAddress2,PostalCity,PostalState,PostalCountry,PostalPostCode,Phone,Mobile,Fax,Email,RegUserID")] Owner owner)
{
    if (ModelState.IsValid)
    {
        db.Owners.Add(owner);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(owner);
}
@Html.HiddenFor(model => model.RegUserID)
但是,保存记录时,RegUserID为空

如果我在@Html Helper上断开,则该值被分配给model.RegUserID,我可以在视图中看到用户ID:

ViewBag.RegUser "7318611e-7e2e-4ee2-9c7b-51b20f0806d8"  dynamic {string}

我做错了什么?

为什么不创建一个空的Owner对象并发送get方法,而不是像这样在ViewBag中传递它:

// GET: Owners/Create
public ActionResult Create()
{
    Owner owner = new Owner();
    owner.RegUserId = User.Identity.GetUserId();
    return View(owner);
}
意见如下:

// GET: Owners/Create
public ActionResult Create()
{
    ViewBag.RegUser = User.Identity.GetUserId();
    return View();
}
@Html.HiddenFor(model => model.RegUserID, new { @value = ViewBag.RegUser })
// POST: Owners/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "OwnerID,OwnerName,ContactName,PhysicalAddress1,PhysicalAddress2,PhysicalCity,PhysicalState,PhysicalCountry,PhysicalPostCode,PostalAddress1,PostalAddress2,PostalCity,PostalState,PostalCountry,PostalPostCode,Phone,Mobile,Fax,Email,RegUserID")] Owner owner)
{
    if (ModelState.IsValid)
    {
        db.Owners.Add(owner);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(owner);
}
@Html.HiddenFor(model => model.RegUserID)

无法通过HTML帮助程序为强类型帮助程序指定值

注意:必须将该值指定给属性,然后将其用作隐藏字段。下面给出了一个例子

在您的视图中,创建如下代码

@{
    model.RegUserID = ViewBag.RegUser;
}
@{
    @Html.HiddenFor(model => model.RegUserID)
}
然后创建一个隐藏字段,如下所示

@{
    model.RegUserID = ViewBag.RegUser;
}
@{
    @Html.HiddenFor(model => model.RegUserID)
}

在我看来,根本不要添加隐藏字段。在
[HttpPost]Create
中,您可以使用与在
[HttpGet]Create
中相同的方式访问它

// GET: Owners/Create
public ActionResult Create()
{
    return View();
}

// remove RegUserID from Bind Include
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "OwnerID,OwnerName,ContactName,PhysicalAddress1,PhysicalAddress2,PhysicalCity,PhysicalState,PhysicalCountry,PhysicalPostCode,PostalAddress1,PostalAddress2,PostalCity,PostalState,PostalCountry,PostalPostCode,Phone,Mobile,Fax,Email")] Owner owner)
{
    if (ModelState.IsValid)
    {
        owner.RegUser = User.Identity.GetUserId();
        db.Owners.Add(owner);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(owner);
}

与此类似,客户端无法更改
RegUserID
字段。

为什么要这样做,而不是在控制器中设置模型值并将模型传递给视图(无论如何都应该这样做)请显示您的ViewModel
Owner
类和
@html.HiddenFor
帮助程序中呈现的html。还建议您删除可笑的
[Bind(Include=“”)]
属性。默认情况下,所有属性都是绑定的,如果不希望所有属性都使用
[Bind(Exclude=“…”)]
或更好的方法,请使用视图模型[Bind…是在首先使用数据库创建项目时添加的。我是asp.net和MVC新手,因此不知道有什么理由删除它。@StephenMuecke我已经完全删除了绑定。谢谢。如果我这样做,我需要更改视图或POST请求吗?不需要太多,只需要使用Model.RegUser就可以了。而且我也不知道你为什么要这么做在POST方法中提到绑定。通常MVC会绑定您的模型对象。我尝试了这个方法,但效果很好,但下面的答案更清晰。没关系,无论哪个方法对您最有帮助。我只是好奇,您表示希望将UserID传递到GET方法中,但您选择的答案没有传递。实际场景是什么?我正在附加一个用户标识两个表中的一个(所有者或服务公司,取决于注册时的选择)其中包含更多关于它们的信息,并链接到其他表。问题只询问如何将用户ID传递到GET方法,因为我正在学习这些内容,并且不知道更多:-)。幸运的是,你们非常慷慨地提供了建议并帮助我理解。非常感谢!