我正在尝试从Asp.net MVC中的Enum创建下拉列表。使用下面发布的代码,我遇到了错误。
这是代码:
class UserTimelineAPIView(MultipleModelAPIView):
def get_queryList(self):
user = self.request.user.id
obj = SubscriberFollower.objects.filter(user=user)
for ab in obj:
queryList = [
(Post.objects.filter(institute_id=ab.institute.id), PostSerializer),
(Event.objects.filter(institute=ab.institute.id, active=True), EventSerializer),
]
return queryList
我的控制器:
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.SenderType, null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.SenderType, "", new { @class = "text-danger" })
</div>
型号:
public ActionResult Create([Bind(Include = "SenderId,SenderName,SenderType,SenderPurpose,UserId")] SenderModel senderModel)
{
try
{
if (ModelState.IsValid)
{
var currentUserId = User.Identity.GetUserId();
senderModel.UserId = int.Parse(currentUserId);
db.Sender.Add(senderModel);
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (DataException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}
return View(senderModel);
}
我发布了我的代码。帮助将不胜感激
答案 0 :(得分:3)
由于您的剃刀中使用了SenderType
属性作为字符串类型,EnumDropDownListFor
不支持。 EnumDropDownListFor
期望枚举类型属性,因此您的SenderType
属性应为SenderType
枚举类型。所以,你的模型应该是:
public class SenderModel
{
[Key]
public int SenderId { get; set; }
[Display(Name = "Sender Name")]
public string SenderName { get; set; }
[Display(Name = "Sender Type")]
public SenderType SenderType { get; set; } // SenderType should be of type enum SenderType
[Display(Name = "Sender Purpose")]
public string SenderPurpose { get; set;}
public int UserId { get; set; }
public virtual ApplicationUser User { get; set; }
}