Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

How to resolve the error 'System.Int32' type to the 'System.String' type is not valid in mvc api ?

$
0
0

Hi

I have an error 'System.Int32' type to the 'System.String' type is not valid in mvc api .

Pls advice me

Thank you
Maideen

It is my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace advEditorials.Models
{
    public class MoDetails
    {
        public int Id { get; set; }
        public string DocNo { get; set; }
        public DateTime PubDate { get; set; }
        public string MainSection { get; set; }
        public string SubSection { get; set; }
        public string Position { get; set; }
        public string Color { get; set; }
        public decimal SizeH { get; set; }
        public decimal SizeW { get; set; }
        public decimal Volume { get; set; }
        public decimal PageNumber { get; set; }
        public string Materials { get; set; }
        public string Remarks { get; set; }
        public string Status { get; set; }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace advEditorials.Models
{
    public interface IMORepository
    {
        Task Add(MoDetails mo);
        Task Update(MoDetails mo);
        Task Delete(string id);
        Task<MoDetails> Getmo(string id);
        Task<IEnumerable<MoDetails>> GetmoALL();
    }
}
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace advEditorials.Models
{
    public class MORepository :IMORepository
    {
        private readonly SqlDbContext db = new SqlDbContext();

// Retrive All
        public async Task<IEnumerable<MoDetails>> GetmoALL()
        {
            try
            {
                var mo = await db.tbl_Editorial.ToListAsync();
                return mo.AsQueryable();
            }
            catch
            {
                throw;
            }
        }

 // by ID
        public async Task<MoDetails> Getmo(string id)
        {
            try
            {
                MoDetails mo = await db.tbl_Editorial.FindAsync(id);
                if (mo == null)
                {
                    return null;
                }
                return mo;
            }
            catch
            {
                throw;
            }
        }
 //Add
        public async Task Add(MoDetails mo)
        {
            mo.Id = Guid.NewGuid().ToString();
            db.tbl_Editorial.Add(mo);
            try
            {
                await db.SaveChangesAsync();
            }
            catch
            {
                throw;
            }
        }

        //Update
        public async Task Update(MoDetails mo)
        {
            try
            {
                db.Entry(mo).State = EntityState.Modified;
                await db.SaveChangesAsync();
            }
            catch
            {
                throw;
            }
        }
//Delete
        public async Task Delete(string id)
        {
            try
            {
                MoDetails mo = await db.tbl_Editorial.FindAsync(id);
                db.tbl_Editorial.Remove(mo);
                await db.SaveChangesAsync();
            }
            catch
            {
                throw;
            }
        }

        private bool MOExists(string id)
        {
            return db.tbl_Editorial.Count(e => e.Id == id) > 0;
        }
    }
} 
Controller

using advEditorials.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace advEditorials.Controllers
{
    public class EditorialController : ApiController
    {
        private readonly IMORepository _iMORepository = new MORepository();

        [HttpGet]
        //[Route("api/Editorial/Get")]
        public async Task<IEnumerable<MoDetails>> Get()
        {
            return await _iMORepository.GetmoALL();
        }

        [HttpPost]
        [Route("api/Editorial/Create")]
        public async Task CreateAsync([FromBody]MoDetails mo)
        {
            if (ModelState.IsValid)
            {
                await _iMORepository.Add(mo);
            }
        }

        [HttpGet]
        [Route("api/Editorial/Details/{id}")]
        public async Task<MoDetails> Details(string id)
        {
            var result = await _iMORepository.Getmo(id);
            return result;
        }

        [HttpPut]
        [Route("api/Editorial/Edit")]
        public async Task EditAsync([FromBody]MoDetails mo)
        {
            if (ModelState.IsValid)
            {
                await _iMORepository.Update(mo);
            }
        }

        [HttpDelete]
        [Route("api/Editorial/Delete/{id}")]
        public async Task DeleteConfirmedAsync(string id)
        {
            await _iMORepository.Delete(id);
        }
    }
}
db Table

CREATE TABLE [dbo].[tbl_Editorial](
	[id] [nvarchar](20) NOT NULL,
	[DocNo] [varchar](20) NULL,
	[pubdate] [date] NULL,
	[MainSection] [varchar](50) NULL,
	[SubSection] [varchar](50) NULL,
	[Position] [varchar](50) NULL,
	[Color] [varchar](20) NULL,
	[SizeH] [float] NULL,
	[SizeW] [float] NULL,
	[Volume] [float] NULL,
	[PageNumber] [float] NULL,
	[Materials] [varchar](100) NULL,
	[Remarks] [varchar](100) NULL,
	[Status] [varchar](20) NULL,
	[Category] [varchar](50) NULL
) ON [PRIMARY]
GO



Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>