Entfernen Sie null-Eigenschaften eines Objekts geschickt, um Json MVC

namespace Booking.Areas.Golfy.Models
{
    public class Time
    {
        public string   time            { get; set; }
        public int      holes           { get; set; }
        public int      slots_available { get; set; }
        public decimal? price           { get; set; }
        public int?     Nextcourseid    { get; set; }

        public bool ShouldSerializeNextcourseid
        {
            get
            {
                return this.Nextcourseid != null;
            }
        }

        public bool? allow_extra { get; set; }

        public bool ShouldSerializeallow_extra
        {
            get
            {
                return this.allow_extra != null;
            }
        }
    }
}

namespace Booking.Areas.Golfy.Controllers
{
    public class TeetimesController : Controller
    {
        //
        //GET: /Golfy/Teetimes/
        public ActionResult Index(
            string date,
            int?   courseid = null,
            int?   clubid = null,
            int?   holes = null,
            int?   available = null,
            int?   prices = null
        )
        {
            var DateFrom = DateTime.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture);

            Teetimes r = BookingManager.GetBookings(new Code.Classes.Params.ParamGetBooking()
            {
                ClubID = clubid
                , DateFrom = DateFrom
                , DateTo = DateFrom.AddDays(1)
                , GroundID = courseid
            });

            return Json(r, JsonRequestBehavior.AllowGet);
        }
    }
}

Den webservice oben gibt einen json-string mit mehreren Herzen lag Zeit.

Möchte ich Eigenschaften Nextcourseid und allow_extra werden Links aus der Ausgabe, wenn Ihre Werte sind null.

Versuchte ich ShouldSerializeXxx aber es scheint nicht zu funktionieren.

Zur info: ich habe auch versucht [ScriptIgnore] die Arbeit ist aber nicht Bedingung.

InformationsquelleAutor Serge | 2014-01-07
Schreibe einen Kommentar