Reputation: 57
I want to use similar kind of action like Exit sub
in MVC application and I am using c# language.
When I just type return
its shows an error. Its ask for compulsory ActionResult
.
[HttpPost]
public ActionResult Create(Location location)
{
if (ModelState.IsValid)
{
Validations v = new Validations();
Boolean ValidProperties = true;
EmptyResult er;
string sResult = v.Validate100CharLength(location.Name, location.Name);
if (sResult == "Accept")
{
ValidProperties = true;
}
else
{
//What should I write here ?
//I wan to write return boolean prperty false
// When I write return it asks for the ActionResult
}
if (ValidProperties == true)
{
db.Locations.Add(location);
db.SaveChanges();
return RedirectToAction("Index");
}
}
ViewBag.OwnerId = new SelectList(
db.Employees, "Id", "FirstName", location.OwnerId);
return View(location);
}
Upvotes: 0
Views: 3547
Reputation: 11964
If i understand what you do in your method, you could try that:
[HttpPost]
public ActionResult Create(Location location)
{
if (ModelState.IsValid)
{
Validations v = new Validations();
Boolean ValidProperties = true;
EmptyResult er;
string sResult = v.Validate100CharLength(location.Name, location.Name);
if (sResult == "Accept")
{
ValidProperties = true;
}
else
{
ValidProperties = false;
ModelState.AddModelError("", "sResult is not accepted! Validation failed");
}
if (ValidProperties == true)
{
db.Locations.Add(location);
db.SaveChanges();
return RedirectToAction("Index");
}
}
ViewBag.OwnerId = new SelectList(
db.Employees, "Id", "FirstName", location.OwnerId);
return View(location);
}
By the way, there are many places to refactoring in this method.
Upvotes: 1
Reputation: 3199
If a method is declared to return any type other than void you can not exit it with return instruction and you must provide a return type. Returning null is usually the answer. In MVC however you might want to return something that will indicate to the user that something went wrong.
Upvotes: 0