开发者

how to handle this type of things. using asp.net mvc

开发者 https://www.devze.com 2022-12-31 00:36 出处:网络
I have public jsonresult update(studentinfo s) { for(i=0;i>0;i++) { var x = // i am getting some x so i am checking again

I have

public jsonresult update(studentinfo s)
{

  for(i=0;i>0;i++)
  {
     var x = // i am getting some x so i am checking again 

     if( x != null)
      {
        var updateuser = student.update(s.student,"","");
        **return json(updateuser.ToString());** // if i keep it here i am getting exceptoin  saying not all code paths return value bec this return i can not keep it out for loop bec each and evary updateuser i need to return json..
     开发者_C百科 }
  }

}

How to overcome this type of things?


What language are you using to write your code? What you've posted doesn't look like any of the valid languages I know for .NET. Here's how the controller action might look in C# (assuming this is the language you are using):

public ActionResult Update(StudentInfo s)
{
    // create some collection that will contain all updated users
    var updatedUsers = new List<StudentInfo>();

    // Revise the loop as it is absolutely not clear from your code
    // what you are trying to do. The way you wrote the loop it will 
    // never execute - for(int i=0; i>0; i++)
    for (int i = 0; i < 5; i++)
    {
        var updatedUser = student.Update(s.student, "", "");
        updatedUsers.Add(updatedUser);
    }

    // return the list of updated users outside the loop so that the compiler 
    // doesn't complain about paths of the method not returning a value
    return Json(updatedUsers);
}


If I understand correctly, you want to return a collection of users. The 'return' keyword does not work like that. You need to return the entire collection at once.

0

精彩评论

暂无评论...
验证码 换一张
取 消