Answer the question
In order to leave comments, you need to log in
What responses are best given from the server?
For example
[HttpPost]
public ActionResult Create(ViewModel vm)
{
if (ModelState.IsValid)
{
var model = Mapper.Map<ViewModel, Post>(vm);
ps.CreatePost(model);
ps.SavePost();
RedirectToRoute("Slug", new { controller = "Log", action = "Details", id = model.Id, slug = model.Slug });
}
return View(vm);
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Post model = ps.GetPostById(id.Value);
if (model == null)
{
return HttpNotFound();
}
var vm = Mapper.Map<Post, PostViewModel>(model);
return View(vm);
}
Answer the question
In order to leave comments, you need to log in
Everything is correct. If you think about the meaning of the method, it should receive the id of the entry and return either its contents or the flag of its absence.
If you replace int?
with int
and remove the first check, the application will work exactly the same, but the error handling will be handled automatically by the framework. It will look for a method with a matching signature, fail to find one, and issue a standard error message.
Thus, the zero value id
for the logic is not important - this is done to more clearly show how errors can be handled manually.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question