我不确定这个标题是否合适,但我不认为更好的标题。
经过多次搜索,我没有找到解决方案。
我是 MVC.NET 的新手。 我构建了一个 Web 应用程序并添加了一个 dll 引用,我想在 View 中使用它。 我成功地使用了我在 View 中构建的类。
模型:
using Mydll;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyWebApplication.Models
{
public class myClass
{
public VaultsOnServer vaults { get; set; }
private MFilesServerApplication oServerApp = new
MFilesServerApplication();
private MFServerConnection oVaultConnection = new
MFServerConnection();
private Vault vault { get; set; }
public bool checkIfLoginAccountExist(string i_Username)
{
LoginAccount loginAccount = null;
loginAccount =
oServerApp.LoginAccountOperations.GetLoginAccount(i_Username);
return loginAccount != null ? true : false;
}
public VaultsOnServer getAllVaults()
{
vaults = oServerApp.GetVaults();
return vaults;
}
}
}
Controller :
using MyWebApplication.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mydll;
namespace MyWebApplication.Controllers
{
public class HomeController : Controller
{
myClass myclass= null;
User user = null;
public ActionResult Home()
{
return View("~/Views/Home/Login.cshtml");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CheckUser(User objUser)
{
VaultsOnServer vaults = null;
bool ifUserExist = false;
if (ModelState.IsValid && InputValidation.onlyLetters(objUser.Username) && InputValidation.checkPassword(objUser.Password))
{
if (objUser != null)
{
if (myclass== null)
{
myclass= new myClass();
}
ifUserExist = mfiles.checkIfLoginAccountExist(objUser.Username);
if(ifUserExist == true)
{
user = objUser;
vaults = myclass.getAllVaults();
Session["Vaults"] = vaults;
}
return View("~/Views/Home/ChooseVault.cshtml", vaults);
}
}
return View("~/Views/Home/Login.cshtml");
}
[HttpPost]
public ActionResult Login(VaultOnServer vault)
{
if(user != null)
{
return View();
}
return View();
}
}
}
View :
@{
ViewBag.Title = "CooseVault";
}
<h2>CooseVault</h2>
@using (Html.BeginForm("Login", "Home", FormMethod.Post))
{
<div id="formCheckUser">
<fieldset id="loginAccount">
<legend id="loginTitle">Choose Vault</legend>
<select id="dropdownOne" name="dropdownOne">
<option value="">--pick--</option>
@foreach (var item in )
{
<option value="@item" selected="selected">@item.Name</option>
}
</select>
</fieldset>
</div>
}
在 Controller 中,我发送了函数 CheckUser 来查看 dll(Mydll) 中类类型的对象,我想在 View 中使用类类型。
我可以这样做吗?
谢谢
最佳答案
你可以通过两种方式做到这一点//首先更好
1) 在网络应用程序中创建另一个类并将 dll 类转换为您的网络应用程序类并按照@Emad 所说在 View 中使用它
public class VaultsOnServerViewModel
{
//some props
}
return View("~/Views/Home/ChooseVault.cshtml", new VaultsOnServerViewModel(vaults));
2) 通过在 views 文件夹的 webconfig 中添加命名空间来添加对 View 的 dll 引用//不要这样做
<add namespace="Mydll" />
然后你就可以在 View 中使用了
@model Mydll.VaultsOnServer
和
@foreach (var item in Model)
3) 你也可以使用字典或 SelectListItem 来做到这一点
return View("~/Views/Home/ChooseVault.cshtml", vaults.Select(s => new SelectListItem() { Text = s.Name, Value = s.Value}).ToList());
在视野中
@model List<SelectListItem>
@foreach (var item in Model)
{
<option value="@item.Value" selected="selected">@item.Text</option>
}
或者使用 DropDownListFor 助手
关于c# - 在 View MVC.NET 中使用来自导入引用的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45049189/