Click here to Skip to main content
15,888,022 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am new in mvc3 i have added my code please resolve this
I am getting this error while click on button in edit form
"Unable to cast object of type 'System.Int32' to type 'System.String'."

my db is
SQL
CharityID int
City varchar(20)

ALTER PROCEDURE [dbo].[Usp_Charities_Federations_Update]  
(    
@CharityID int,

 @City        varchar(50)    

)        
      
AS        
Begin  
  Update Charity set

 City=  @City

End    

execute Usp_Charities_Federations_Update 50,'Gajuwak'


This is Controller:
C#
#region AcctionLink for EditCharity START
        [HttpGet]
        public ActionResult AddCharity(int charityID=0)
        {
            CharityModel objCharityModel = new CharityModel();
            objCharityModel = GetCharityRecord(charityID);
            
            return View(objCharityModel);
        }

        [HttpPost]
        public ActionResult AddCharity(CharityModel objCharityModel)
        {
            if (!ModelState.IsValid)
            {
                UpdateCharity(objCharityModel);
            }
            return View(objCharityModel);
        }
        public CharityModel GetCharityRecord(int cahrityID)
        {
            CharityModel objCharityModel = new CharityModel();
            var charityDetail = objEntities.Charities.Where(m => m.CharityID == cahrityID);
            if (charityDetail != null)
            {
                foreach (var item in charityDetail)
                {
                    objCharityModel.PCharityId = item.CharityID;
                   
                  
                    objCharityModel.PCity=item.City;
                 
                
                }
            }
            return objCharityModel;
        }

        public void UpdateCharity(CharityModel objCharityModel)
        {
            try
            {
                //Call your update function to update record
                objEntities.Update(
                    objCharityModel.PCharityId,
                  
                    objCharityModel.PCity ="Gajuwaka"
                   
                   
                    );
                ViewData["updatestatus"] = "1";
            }
            catch
            {
                ViewData["updatestatus"] = "0";
            }
        }
        #endregion

This is Model:
C#
public class CharityModel
   {

     
      
       public int PCharityId { get; set; }
     
      
       public string PCity{ get; set; }



This is View:

C#
@model PledgeNetApplication.Models.CharityModel
  <tr>
                                <td>
                                    CharityID:
                                </td>
                                <td style="width: 412px;">
                                    <div>
                                        @Html.TextBoxFor(m => m.PCharityId)
                                        
                                        <div class="inlineMessage">
                                        </div>
                                        <div style="clear: both">
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Charity Number:
                                </td>
                                <td style="width: 412px;">
                                    @Html.TextBoxFor(m => m.PCity) @Html.ValidationMessageFor(model => model.PCharityName)
                                </td>
                                <td id="PCharityName">
                                </td>
                            </tr>
<save button="">

while execute this data will be binded but when i update this error will appear.

C#
"Unable to cast object of type 'System.Int32' to type 'System.String'."

My sp is executed successufully but from front the data will not be updated

so please help me.
Posted
Updated 7-Feb-13 21:44pm
v2
Comments
Herman<T>.Instance 8-Feb-13 3:44am    
have you tried:
@Html.TextBoxFor(m => m.PCharityId.ToString())

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900