单选按钮是只读使用枚举?

时间:2013-07-22 13:38:19

标签: c# asp.net sql html5 asp.net-mvc-4

在开发ASP.Net MVC 4应用程序时,我已经按照pass enum to html.radiobuttonfor MVC3上的答案和答案得出了答案,但我的问题仍然存在。

我正在创建一个编辑页面,并希望将状态显示为两个单选按钮“Active”和“InActive”,这些值将从数据库中读取为Enums 1 = Active,2 = InActive。

问题是,当页面显示时,它显示选择了与数据库值对应的正确单选按钮,但是不允许用户更改/选择其他单选按钮?

任何想法,这让我疯狂 (同时将属性更改为chkbox的bool将导致比此时解决的问题更多的问题。)

控制器.....

   [HttpPost]
    public ActionResult Edit(NewsArticle newsArticle, int id, HttpPostedFileBase Article)
    {
        try
        {
            if (ModelState.IsValid)
            {

             NewsArticle savedArticle= _newsArticle.Get(id);

             savedArticle.Body = newsArticle.Body;
             savedArticle.Title = newsArticle.Title;
             savedArticle.Status = newsArticle.Status;

             if(Article == null)
             {
                 newsArticle.ArticleImage = savedArticle.ArticleImage;
             }
             else
             {
                 using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
                 {
                     newsArticle.ArticleImage = binaryReader.ReadBytes(Request.Files[0].ContentLength);
                 }

                 savedArticle.ArticleImage = newsArticle.ArticleImage;

                 string imgeName = Path.GetFileName(Article.FileName);

                 savedArticle.ImageName = imgeName;

             }



             _uow.SaveChanges();
                return RedirectToAction("Index");
            }

查看..........

<div class="control-group">
    <div class="editor-field">
        <label class="control-label">Select Status :</label>
        <div class="controls">

            @Html.RadioButtonForEnum(n => n.Status)

               </div>
        </div>
    </div>

辅助/扩展.....

      public static MvcHtmlString RadioButtonForEnum<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression
        )
        {
            var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

            var names = Enum.GetNames(metaData.ModelType);
            var sb = new StringBuilder();
            foreach (var name in names)
            {
                var description = name;

                var memInfo = metaData.ModelType.GetMember(name);
                if (memInfo != null)
                {
                    var attributes = memInfo[0].GetCustomAttributes(typeof(DisplayAttribute), false);
                    if (attributes != null && attributes.Length > 0)
                        description = ((DisplayAttribute)attributes[0]).Name;
                }

                var id = string.Format(
                    "{0}_{1}_{2}",
                    htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix,
                    metaData.PropertyName,
                    name
                );

                var radio = htmlHelper.RadioButtonFor(expression, name, new { id = id }).ToHtmlString();
                sb.AppendFormat(
                    "<label for=\"{0}\">{1}</label> {2}",
                    id,
                    HttpUtility.HtmlEncode(name),
                    radio
                );
            }
            return MvcHtmlString.Create(sb.ToString());
        }

帮助器正在显示应从视图中调用的标签。

...枚举

     [Flags]
public enum NewsArticleStatus
{
    [Display(Name = "Active")]
    Active = 1,

    [Display(Name = "InActive")]
    Inactive = 2
}

1 个答案:

答案 0 :(得分:0)

得到它......

将我的原始模型(文章)添加到viewModel并添加了bool属性....活动..

      public bool Active
    {
        get
        {
            if (this.Article != null)
            {
                return (this.Article.Status == NewsArticleStatus.Active);
            }
            return false;
        }
        set
        {
            if (this.Article != null)
            {
                this.Article.Status = (value ? NewsArticleStatus.Active : NewsArticleStatus.Inactive);
            }
        }
    }  

创建chkbox ....

  @Html.CheckBoxFor(n => n.Active)

所以在Controller中只需将值赋给新模型

 newNews.Status = model.Article.Status;