简体   繁体   English

我如何在扩展身份模型中返回单选按钮选定的值

[英]How can i return radiobutton selected value within extended idenity model within

I am new to ASP.net MVC.我是 ASP.net MVC 的新手。 I am stucked right now.我现在被困住了。 i extended the identity model to include bio data like firstName, LastName, Gender etc.我扩展了身份模型以包含诸如名字、姓氏、性别等生物数据。

I want to have the Gender rendered as radio button, I am able to run app without any error, but it will not submit the registration.我想将性别呈现为单选按钮,我能够运行应用程序而不会出现任何错误,但它不会提交注册。 This issue started after i change the gender from textbox to radio button.这个问题是在我将性别从文本框更改为单选按钮后开始的。 here is my code.这是我的代码。

Part of my model:我的模型的一部分:

    [Display(Name = "Middle Name")]
    [MaxLength(25)]
    public string MiddleName { get; set; }

    [Required]
    [Display(Name = "Last Name")]
    [MaxLength(25)]
    public string LastName { get; set; }

    [Required]
    [Display(Name = "Gender")]
    public string Gender { get; set; }

My controller:我的控制器:

 public async Task<ActionResult> Register(RegisterViewModel model)
    {
    if (ModelState.IsValid)
        {


            var member = new MemberInformation
            {
                Id =
                    Guid.NewGuid().ToString() + DateTime.Now.Year +             DateTime.Now.Month + DateTime.Now.Day +
                    DateTime.Now.Hour,
                FirstName = model.FirstName,
                LastName = model.LastName,
                MiddleName = model.MiddleName,
                Gender = model.Gender,
                ContactAddress = model.ContactAddress,
                MarialStatus = model.MarialStatus,
                Occupation = model.Occupation,
                MobilePhone = model.MobilePhone,
                RegistrationDate = DateTime.Now,
         }

my view:我的看法:

    <div class="form-group">
    @Html.LabelFor(m => m.Gender, new {@class = "col-md-2 control-label",})
    <div class="col-md-10">
        @Html.LabelFor(m => m.Gender, "Male")
        @Html.RadioButtonFor(Model => Model.Gender,  "Male") 
        @Html.LabelFor(m => m.Gender, "Female")
        @Html.RadioButtonFor(m => m.Gender,  "Female")
      </div>
     </div>

I suspect your Model => Model.Gender expression is causing some confusion because Model already means something in that scope.我怀疑你的Model => Model.Gender表达式引起了一些混乱,因为 Model 已经意味着该范围内的某些东西。

LabelFor is also odd when used that way, use an html label to simplify things LabelFor 以这种方式使用时也很奇怪,使用 html 标签来简化事情

    <label>@Html.RadioButtonFor(m => m.Gender, "Male")Male</label>
    <label>@Html.RadioButtonFor(m => m.Gender, "Female")Female</label>

When you use Html.RadioButtonFor the same model property twice it creates two controls with the same ID.当您两次使用Html.RadioButtonFor相同的模型属性时,它会创建两个具有相同 ID 的控件。 As the postback only cares about names, not IDs, you need to override the IDs, as below:由于回发只关心名称,而不关心 ID,因此您需要覆盖 ID,如下所示:

@Html.RadioButtonFor(m => m.Gender, "Male", new {id = "GenderMale"})
@Html.RadioButtonFor(m => m.Gender, "Female", new { id = "GenderFemale" }) 

This creates radiobuttons that map to Gender, but with different IDs.这将创建映射到 Gender 的单选按钮,但具有不同的 ID。

Note - you should include the new { id = "Whatever" } bit, otherwise it'll duplicate IDs again.注意 - 您应该包含new { id = "Whatever" }位,否则它会再次复制 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM