0

ASP.Net MVC 4 アプリケーションの開発中に、MVC3 のhtml.radiobutton への列挙型のパスに関する回答と、それが派生した回答に従いましたが、問題は残ります。

編集ページを作成していて、Status を「Active」と「InActive」の 2 つのラジオ ボタンとして表示したいと考えています。これらの値はデータベースから 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
}
4

1 に答える 1