不支持返回类型'System.String'。参数名称:表达式MVC枚举

时间:2017-01-24 05:15:07

标签: c# asp.net-mvc-4 enums

我正在尝试从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);
    }

我发布了我的代码。帮助将不胜感激

1 个答案:

答案 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; }
}