简体   繁体   中英

Asp.net mvc3 How to display byte as string? Asp.net mvc3

I have Gender attribute as tiny int in Db for employee. When user create new employee i want him to choose male/female (which is working properly) by clicking on radio button. Everything is working fine (create and edit) but i want to display in form (for index, details and delete) not 1 or 2, but male/female. there should be some if statement in view but i'm not sure where to put it or how to write correct one ...

any idea? Thanks! this is part of code from model: 在此处输入图片说明

This one is from details.cshtml:

在此处输入图片说明

Use a condition ? true : false condition ? true : false selector

int gender = 1; // assumed male

String genderDesc = (gender == 1) ? "Male" : "Female";

You Could Make this a Drop Down field? In your controller do:

ViewBag.Gender = new[] 
        {
            new SelectListItem { Text = "Male", Value = "1" }, 
            new SelectListItem { Text = "Female", Value = "2" }
        }.WithEmpty();

The WithEmpty() will give you a blank option or without it will select the top one.

@Html.DropDownListFor(m=> m.Gender, (Ienumarable<SelectListItem>)ViewBag.Gender);

This way the user will see Male and Female but the value will be bound to your model using the value which is 1 or 2.

If you want to avoid having extra properties on your model or adding stuff to your viewbag you can write it inline using razor syntax like below..

<div class="display-field">
@if (model.GENDER == 0){ @Html.Raw("Male") }
@else if (model.GENDER == 1){ @Html.Raw("Female") }
</div>

That's off the top of my head so you might need to check the exact syntax but i think that's close. It will also just dump "Male" or "Female" inside the div, you might want to put it in a label or p tag at least.

This however isn't the approach I would use in a production app, throughout the code i would use a gender enum to give meaning to your bit value and extend enum to include a description that you can parse for presentation purposes.

I think from what u have said u can write it inline using the razor syntax like below in ur details.cshtml

<div class="display-field">
@if(model.GENDER ==0)
{
 <label>Male</label>
}
else if(model.Gender==1)
{
 <label>Female</label>
}
</div>

I think this must be enough for displaying the Gender in details page.plz comment if u need any help

I'd advise that you create a ViewModel class to represent the Employee entity in a View-friendly format decoupled from your database model and present it in a strongly typed view so you can return it from the controller.

Hint: Have the Gender property represented as a string in the ViewModel and do the conversion from byte to the string representation in your controller

You may put the code in your controller as eg:

public ActionResult EmployeeDetails(int id)

{

   //retrieve the entity from the DB
   //set other employee properties here.
   //I'm assuming you have set males to 2 and females to 1
   ...

   employeeViewObject.Gender = employeeObjectFromDB.Gender.Value==2?"Male":"Female";
   return View(employeeObjectFromDB);

}

Your strongly typed view will not have trouble displaying the gender while saving you the dirt of mixing code and mark-up as: <p>model.Gender</p> or

Html.DisplayFor(model=>model.Gender)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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