Asp.net mvc MVC-在控制器内设置多个ActionLink和TextBox属性
我希望能够根据控制器内部的条件将多个表单(html)对象(Textbox、ActionLink)设置为visible=false或enabled=false 在web表单中,我将执行下面的代码。在控制器内部的MVC中处理此问题的最佳方法是什么?谢谢Asp.net mvc MVC-在控制器内设置多个ActionLink和TextBox属性,asp.net-mvc,web,permissions,actionlink,Asp.net Mvc,Web,Permissions,Actionlink,我希望能够根据控制器内部的条件将多个表单(html)对象(Textbox、ActionLink)设置为visible=false或enabled=false 在web表单中,我将执行下面的代码。在控制器内部的MVC中处理此问题的最佳方法是什么?谢谢 switch (UserSession.AppUserAccessLevel) { case AccessLevel.FullAdmin: txtLast
switch (UserSession.AppUserAccessLevel)
{
case AccessLevel.FullAdmin:
txtLastName.Enabled = true;
lnkExportData.Visible = true;
btnSubmit.Enabled = true;
break;
case AccessLevel.Admin:
txtLastName.Enabled = true;
lnkExportData.Visible = false;
btnSubmit.Enabled = false;
break;
case AccessLevel.ReadOnly:
lnkExportData.Visible = false;
btnSubmit.Enabled = false;
break;
}
要在MVC中执行此操作,您需要创建用户模型,并通过控制器将模型传递给视图。请在下面找到示例代码。您可以在文本框中循环并使用Razor视图设置属性
Controller Action:
public ActionResult YourAction()
{
//Sets your property
string yourProperty = _db.GetProperties().FirstOrDefault();
//Passing in "yourProperty" as the Model to the View
return View(yourProperty);
}
//Alternativately you can bind the property to a Model that has multiple properties
public ActionResult YourAction()
{
YourModelClass model = new Model();
model.Property = _db.GetProperties().FirstOrDefault();
return View(model);
}
Within your View (Top of your View): (The name should match the name of the Controller Action - so YourAction.cshtml / YourAction.aspx)
//You need to firstly bind your Model to a type.
@Model string //This is your first example above. It simply binds the Model to a string
@Model YourModelClass //This binds the Model (and all of its properties) to the passed in YourModelClass
Populating your Textboxes (Again - within your View)
//Now you can access and populate a textbox in the methods previously listed
@Html.TextBox("TextBoxName",Model) //If using a string for the Model
@Html.TextBox("TextBoxName",Model.YourProperty) //If using YourModelClass as the Model
or
@Html.TextBoxFor(model => model.YourProperty) //Again - using YourModelClass