我是ASP .NET MVC4 RAZOR的初学者,我用:
创建了一个应用程序我的对象是PersonBO
,我在控制器端使用生成的代理PersonClient
调用它,我正在尝试创建Edit
方法,但我不知道如何要调用它,这是我在控制器端尝试的代码:
private ActionResult Edit(PersonBO objPerson)
{
Int32 idRet = -1;
try
{
using (PersonClient PClient = new PersonClient())
{
idRet = JClient.UpdatePerson(objPerson.Id,
objPerson.Name,
objPerson.LastName,
objPerson.BirthdayDate);
}
}
catch (Exception)
{
throw;
}
return View(objPerson);
}
以下是要调用的Edit
:
[HttpPost]
public ActionResult Edit(Int32 id)
{
PersonBO objPerson=new PersonBO ();
using (PersonClient Jclient = new PersonClient())
{
if ((ModelState.IsValid))
{
objPerson= Jclient.GetPersonById(id);
Edit(objPerson);
return RedirectToAction("GetAll");
}
else
{
return View(objPerson);
}
}
}
有人可以帮忙吗,我不知道我做错了什么?
答案 0 :(得分:0)
将您的私有方法重命名为其他方法,例如EditPerosn以防止误解,并将其返回值更改为void或除ActionResult之外的其他内容 您的代码应如下所示:
private Int32 EditPerson(PersonBO objPerson)
{
Int32 idRet = -1;
try
{
using (PersonClient PClient = new PersonClient())
{
idRet = JClient.UpdatePerson(objPerson.Id,
objPerson.Name,
objPerson.LastName,
objPerson.BirthdayDate);
}
}
catch (Exception)
{
}
return idRet;
}
[HttpPost]
public ActionResult Edit(PersonBO objPerson)
{
if (ModelState.IsValid && objPerson != null)
{
Int32 result = EditPerson(objPerson);
if(result != -1)
{
return RedirectToAction("GetAll");
}
else
{
ViewBag.Message = "Update failed!";
return View(objPerson);
}
}
else
{
ViewBag.Message = "Validation error!";
return View(objPerson);
}
}
答案 1 :(得分:0)
您需要2种方法才能执行此操作: 1.获取PersonBO并渲染视图:
[HttpGet]
public ActionResult Edit(Int32 id)
{
PersonBO objPerson = null;
using (PersonClient Jclient = new PersonClient())
{
objPerson= Jclient.GetPersonById(id);
}
if (objPerson != null)
{
return View(objPerson);
} else {
return View("NotFound");
}
}
保存已发布的更改:
[HttpPost]
public ActionResult Edit(PersonBO objPerson)
{
if (!ModelState.IsValid)
{
return View(objPerson);
}
using (PersonClient PClient = new PersonClient())
{
idRet = JClient.UpdatePerson(objPerson.Id,
objPerson.Name,
objPerson.LastName,
objPerson.BirthdayDate);
}
}
这只是一个简单的例子来提出一个想法。如果您可以/希望以某种方式处理它们,请添加异常处理/记录。