gpt4 book ai didi

c# - 将 View 模型中的枚举绑定(bind)到 MVC

转载 作者:行者123 更新时间:2023-11-30 20:37:26 25 4
gpt4 key购买 nike

每当我尝试创建工单时,我的工单优先级也很低。我已经为优先级设置了一个下拉列表,但票证上的优先级分配似乎不起作用。我怀疑它与我的 Views\Ticket\Create.cshtml 有关

我需要一些说明,看看我这样做是否正确:

两者都需要吗

在我的模型中,我有一个名为 Ticket 的类,它具有枚举优先级

public enum Priority
{
Low, Med, High
}
public class Ticket
{
....
}

Configuration.cs(用于插入工单数据的片段)

var tickets = new List<Ticket>
{
new Ticket {
UserID = users.Single(s => s.LastName == "Wan").UserID,
CategoryID = categories.Single(c => c.CategoryName == "Con-X" ).CategoryID,
Issue = ("Con-X Login Error"),
Priority = Priority.High
},
new Ticket {
UserID = users.Single(s => s.LastName == "Wan").UserID,
CategoryID = categories.Single(c => c.CategoryName == "Desktop" ).CategoryID,
Issue = ("Can't remote access C0123"),
Priority = Priority.Med
},
};

在我的 View 模型 TicketVM 中,我还为优先级设置了枚举。由于我正在通过此配置我的票证:

            Ticket ticket = new Ticket
{
UserID = (int)WebSecurity.CurrentUserId,
Issue = model.Issue,
IssuedTo = model.IssuedTo,
CategoryID = model.CategoryID,
Priority = model.priority <-- ERROR
};

错误:

Cannot implicitly convert type 'RecreationalServicesTicketingSystem.ViewModels.TicketVM.Priority' to 'RecreationalServicesTicketingSystem.Models.Priority'. An explicit conversion exists (are you missing a cast?) RecreationalServicesTicketingSystem

Views\Ticket\Create.cshtml

    <div class="form-group">
@Html.DropDownListFor(m => m.priority, Model.PriorityList, "Please select the priority")
</div>

ViewModels\TicketVM.cs

public class TicketVM
{
public int UserID { get; set; }
[Required(ErrorMessage = "Please enter the description")]
public string Issue { get; set; }
[Display(Name = "Administrator")]
[Required(ErrorMessage = "Please select the Administrator")]
public int IssuedTo { get; set; }
public IEnumerable<SelectListItem> AdministratorList { get; set; }
public int CategoryID { get; set; }
public IEnumerable<SelectListItem> CategoryList { get; set; }

public enum Priority
{
LOW = 3,
MED = 2,
HIGH = 1,
}

public Priority priority { get; set; }
public IEnumerable<SelectListItem> PriorityList
{
get
{
var priorityType = typeof(Priority);
var priorityValues = Enum.GetValues(priorityType).Cast<Priority>();
var converter = TypeDescriptor.GetConverter(priorityType);

return
from priorityValue in priorityValues
select new SelectListItem
{
Text = converter.ConvertToString(priorityValue),
Value = priorityValue.ToString(),
};
}
}
}

TicketController.cs

public ActionResult Create()
{
TicketVM model = new TicketVM();
ConfigureViewModel(model);
return View(model);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(TicketVM model)
{
if (!ModelState.IsValid)
{
model.CategoryList = new SelectList(db.Categories, "CategoryID", "CategoryName");
ConfigureViewModel(model);
return View(model);
}
Ticket ticket = new Ticket
{
UserID = (int)WebSecurity.CurrentUserId,
Issue = model.Issue,
IssuedTo = model.IssuedTo,
CategoryID = model.CategoryID,
Priority = model.priority <-- ERROR
};
db.Tickets.Add(ticket);
db.SaveChanges();
return RedirectToAction("Index");
}

private void ConfigureViewModel(TicketVM model)
{
IEnumerable<User> admins = db.Users.Where(u => u.IsAdministrator).OrderBy(u => u.LastName);
model.AdministratorList = admins.Select(a => new SelectListItem
{
Value = a.UserID.ToString(),
Text = string.Format("{0} {1}", a.FirstMidName, a.LastName)
});
IEnumerable<Category> categories = db.Categories.OrderBy(u => u.CategoryName);
model.CategoryList = categories.Select(a => new SelectListItem
{
Value = a.CategoryID.ToString(),
Text = a.CategoryName.ToString()
});
}

最佳答案

enums在你的Ticket模型和 TicketVM型号不一样。

一个选择是在 TicketVM 中添加一个 using 语句这样你就可以使用 enum来自 RecreationalServicesTicketingSystem.Models (并删除 enum 类中的 TicketVM 定义)

@using RecreationalServicesTicketingSystem.Models
public class TicketVM
{
....
public Priority priority { get; set; } // uses the enum from the Models assembly
}

另一种选择是从一个转换到另一个。由于您需要在对象初始值设定项中使用它,因此在 Controller 中创建一个私有(private)方法来转换值

private Models.Priority CastPriority(ViewModels.TicketVM.Priority value)
{
Models.Priority priority = Models.Priority.Low;
Enum.TryParse<Models.Priority>(value.ToString(), out priority);
return priority;
}

在对象初始值设定项中

Ticket ticket = new Ticket
{
....
CategoryID = model.CategoryID,
Priority = CastPriority(model.priority)
};

旁注:您可以简化生成 IEnumerable<SelectListItem> 的代码到

public IEnumerable<SelectListItem> PriorityList
{
get
{
return Enum.GetNames(typeof(Proprity)).Select(e => new SelectListItem() { Text = e, Value = e });
}
}

关于c# - 将 View 模型中的枚举绑定(bind)到 MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36120415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com