下拉和单选按钮列表的字符串字段在MVC 4

本文关键字:字段 MVC 字符串 单选按钮 列表 | 更新日期: 2023-09-27 18:17:28

我有字符串字段的性别和医院类别在我的MVC 4(代码第一)模型类。我想有一个单选按钮列表的性别显示男性和女性和保存"男性"或"女性"根据选择在我的视图,而不是生成视图时添加的文本框。而对于医院,我想显示医院类别的一个d下拉列表,例如,政府,私人等,一个选择将被保存到医院类别。我知道我是否使用另一个类并通过ID字段建立关系。但由于我已经在这些领域的数据和应用程序正在运行,我正在寻找任何快捷方式。请帮帮我。

模型类:

    public class MedicalDetail
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    public string CaseNo
    {
        get
        {
            if (this.RimsMain != null)
                return this.RimsMain.CaseNoNew;
            else
                return "N/A";
        }
    }
    [Required]
    [ForeignKey("RimsMain")]
    public int RimsMainId { get; set; }
    public virtual RimsMain RimsMain { get; set; }
    [Required]
    public string Name { get; set; }
    [DisplayFormat(DataFormatString = "{0:d}")]
    [Column(TypeName = "DateTime2")]
    public DateTime Dob { get; set; }
    public string Nationality { get; set; }
    [DisplayFormat(DataFormatString = "{0:d}")]
    [Column(TypeName = "DateTime2")]
    [DisplayName("Examin Date")]
    public DateTime ExaminDate { get; set; }
    public string Gender { get; set; }
    [DisplayName("Referred For Hospital")]
    public string ReferredForHospital { get; set; }
    [DisplayName("Madicine Provided")]
    public string MadicineProvided { get; set; }
    public string Demand { get; set; }
    public string Reimbursement { get; set; }
    [DisplayName("Milk Assistance")]
    public string MilkAssistance { get; set; }
    [DisplayName("Chronic Case")]
    public string ChronicCase { get; set; }
    [DisplayName("Special Case")]
    public string SpecialCase { get; set; }
    [DisplayName("Gyne Case")]
    public string GyneCase { get; set; }
    [DisplayName("Referred By UNHCR")]
    public bool ReferralByUNHCR { get; set; }
    [MaxLength]
    public string LabInvestigation { get; set; }
    [MaxLength]
    public string Diagnosis { get; set; }
    public string HospitalCategory { get; set; }
    [MaxLength]
    public string Recommendation { get; set; }
    [DisplayName("Last Updated by")]
    public int? UserId { get; set; }
    [DisplayFormat(DataFormatString = "{0:d}")]
    [Column(TypeName = "DateTime2")]
    [DisplayName("Last Updated")]
    public DateTime? LastUpdated { get; set; }

}

视图:

        @model Rims.Models.MedicalDetail
@{
    ViewBag.Title = "Create";
}
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Medical Detail</legend>
        <h4 style="color:@ViewBag.Color;">@ViewBag.Message</h4>
        <div>
            <div class="col2">
                <div class="editor-label">
                    <label for="RimsMainId">Case No</label>
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.RimsMain.CaseNoNew)
                    @Html.ValidationMessageFor(model => model.RimsMain.CaseNoNew)
                </div>
                <div class="editor-label">
                    <label for="Name">Name</label>
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Name)
                    @Html.ValidationMessageFor(model => model.Name)
                </div>
                <div class="editor-label">
                    <label for="Dob">DOB</label>
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(model => model.Dob, new { @class = "form-control datepicker" })
                    @Html.ValidationMessageFor(model => model.Dob)
                </div>
                <div class="editor-label">
                    <label for="Nationality">Nationality</label>
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Nationality)
                    @Html.ValidationMessageFor(model => model.Nationality)
                </div>
                <div class="editor-label">
                    <label for="ExaminDate">Examin Date</label>
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(model => model.ExaminDate, new { @class = "form-control datepicker" })
                    @Html.ValidationMessageFor(model => model.ExaminDate)
                </div>
                <div class="editor-label">
                    <label for="Gender">Gender</label>
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Gender)
                    @Html.ValidationMessageFor(model => model.Gender)
                </div>
            </div>
            <div class="col2">
                <div>
                    <div class="editor-label">
                        <label for="ReferredFor">Referred by UNHCR</label>
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.ReferralByUNHCR)
                        @Html.ValidationMessageFor(model => model.ReferralByUNHCR)
                    </div>
                    <div class="clear"></div>
                </div>
                <div class="editor-label">
                    <label for="MadicineProvided">Madicine Provided</label>
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.MadicineProvided)
                    @Html.ValidationMessageFor(model => model.MadicineProvided)
                </div>
                <div class="editor-label">
                    <label for="MilkAssistance">Milk Assistance</label>
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.MilkAssistance)
                    @Html.ValidationMessageFor(model => model.MilkAssistance)
                </div>
                <div class="editor-label">
                    <label for="MilkAssistance">Chronic Case</label>
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.ChronicCase)
                    @Html.ValidationMessageFor(model => model.ChronicCase)
                </div>
                <div class="editor-label">
                    <label for="MilkAssistance">Special Case</label>
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.SpecialCase)
                    @Html.ValidationMessageFor(model => model.SpecialCase)
                </div>
                <div class="editor-label">
                    <label for="MilkAssistance">Gyne Case</label>
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.GyneCase)
                    @Html.ValidationMessageFor(model => model.GyneCase)
                </div>
            </div>
            <div class="clear"></div>
        </div>
        <div class="editor-label">
            <label for="Demand">Demand</label>
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Demand, 5, 70, new { @class = "txtArea" })
            @Html.ValidationMessageFor(model => model.Demand)
        </div>
        <div class="editor-label">
            <label for="ReimbursemenT">Reimbursement</label>
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Reimbursement, 5, 70, new { @class = "txtArea" })
            @Html.ValidationMessageFor(model => model.Reimbursement)
        </div>
        <div class="editor-label">
            <label for="Referred">Referred for Hospital</label>
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.ReferredForHospital, 5, 70, new { @class = "txtArea" })
            @Html.ValidationMessageFor(model => model.ReferredForHospital)
        </div>
         <div class="editor-label">
            <label for="Remarks">Lab Investigation</label>
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.LabInvestigation, 5, 70, new { @class = "txtArea" })
            @Html.ValidationMessageFor(model => model.LabInvestigation)
        </div>
         <div class="editor-label">
            <label for="Remarks">Diagnosis</label>
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Diagnosis, 5, 70, new { @class = "txtArea" })
            @Html.ValidationMessageFor(model => model.Diagnosis)
        </div>
         <div class="editor-label">
            <label for="Remarks">Hospital Category</label>
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.HospitalCategory, 5, 70, new { @class = "txtArea" })
            @Html.ValidationMessageFor(model => model.HospitalCategory)
        </div>
        <div class="editor-label">
            <label for="Remarks">Recommendation</label>
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Recommendation, 5, 70, new { @class = "txtArea" })
            @Html.ValidationMessageFor(model => model.Recommendation)
        </div>
        <p id="otherButtons1">
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<div id="otherButtons">
    @Html.ActionLink("Back to List", "Index")
</div>

在上面的视图中,我想用两个单选按钮(Male和Female)替换以下字段

<div class="editor-label">
                    <label for="Gender">Gender</label>
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Gender)
                    @Html.ValidationMessageFor(model => model.Gender)
                </div>

和The Following字段通过下拉列表,其中将包含一些值。例如"Gov", "Private"等

 <div class="editor-label">
            <label for="Remarks">Hospital Category</label>
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.HospitalCategory, 5, 70, new { @class = "txtArea" })
            @Html.ValidationMessageFor(model => model.HospitalCategory)
        </div>

我也想知道如何保存值在创建动作:

[HttpPost]
        [Authorize(Roles = "Admin,Medical")]
        [ValidateAntiForgeryToken]
        public ActionResult Create(MedicalDetail medicaldetail)
        {
            if (ModelState.IsValid)
            {
                var rimsMain = db.RimsMains.Where(x => x.CaseNoNew == medicaldetail.RimsMain.CaseNoNew).FirstOrDefault();
                if (rimsMain == null)
                {
                    ViewBag.Message = "Invalid Case Number!";
                    ViewBag.Color = "Red";
                    return View(medicaldetail);
                }
                else
                {
                    medicaldetail.RimsMainId = rimsMain.Id;
                    //medicaldetail.RimsMain = rimsMain;
                    medicaldetail.LastUpdated = DateTime.Now;
                    medicaldetail.UserId = WebSecurity.CurrentUserId;
                    db.MedicalDetails.Add(medicaldetail);
                    db.SaveChanges();
                    return RedirectToAction("Create", new { message = "Record Added Successfully!", color = "Green" });
                }
            }
            return View(medicaldetail);
        }

下拉和单选按钮列表的字符串字段在MVC 4

对于性别选择,您需要使用同名的单选按钮::

<input type="radio" name="Gender" value="Male"/>Male
<br/>
<input type="radio" name="Gender" value="Female"/>Female

对于Company列表,您应该使用viewbag as::

@Html.DropDownList("Company", "Select Company")

,在服务器端Viewbag将形成为::

ViewBag.Company = new SelectList(context.Companies, "ID", "Name");