亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從其他位置在 Razor 頁面中添加模型

如何從其他位置在 Razor 頁面中添加模型

C#
MMMHUHU 2022-12-31 13:23:47
我的模型public class UserRegistrationViewModel{    public string Description { get; set; }}指數@model UserRegistrationViewModel<form asp-controller="Demo" asp-action="RegisterTextArea" method="post">    <textarea asp-for="Description"></textarea>    <button type="submit">Test</button></form>那里很完美,但是如果我在類庫項目中聲明了我的實體域,會發生什么情況?如何將那個類的地址放在我的剃刀頁面上?這句話對嗎?我目前犯了一個錯誤@model DomainEntities.ViewModel.UserRegistrationViewModel 另一個例子我目前正在犯錯誤@model Domain.ViewModel.UserLogInViewModel@{    ViewBag.Title = "Login";}<h2>Login</h2><form method="post" asp-controller="Account" asp-action="Login"      asp-route-returnurl="@Model.ReturnUrl">    <div asp-validation-summary="ModelOnly"></div>    <div>        <label asp-for="Username"></label>        <input asp-for="Username" />        <span asp-validation-for="Username"></span>    </div>    <div>        <label asp-for="Password"></label>        <input asp-for="Password" />        <span asp-validation-for="Password"></span>    </div>    <div>        <label asp-for="RememberMe"></label>        <input asp-for="RememberMe" />        <span asp-validation-for="RememberMe"></span>    </div>    <div>        <input type="submit" value="Login" />    </div></form>
查看完整描述

1 回答

?
SMILET

TA貢獻1796條經驗 獲得超4個贊

所以如果你的Web項目直接引用你的DomainEntities項目那么是的,它會起作用你只需要指定完整namespaceViewModel

@model My.Full.Namespace.DomainEntities.ViewModel.UserRegistrationViewModel

或者更清潔:

@using My.Full.Namespace.DomainEntities.ViewModel
@model UserRegistrationViewModel

對于第二個錯誤,你的namespaceto the ViewModelis different, ie DomainvsDomainEntities很可能是你namespaces寫錯了或者拼寫不完整。

如果您使用某種方式Dependency Injection來注入您DomainEntities,那么如果沒有一些瘋狂的解決方法,您的方式將行不通。Web需要直接依賴于您的項目ViewModels。

最后,我質疑你把你ViewModels放在一個單獨的項目中,我會說除了最極端的情況外,你ViewModels應該在你的Web項目中。


查看完整回答
反對 回復 2022-12-31
  • 1 回答
  • 0 關注
  • 123 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號