Свежий ASP-MVC'ер здесь.
Моя модель: Applications.cs имеет обязательное строковое поле: имя.
MainView, showApps, показывает все приложения в БД в виде таблицы... вычистил код:
<tbody>
@foreach (var app in Model)
{
<tr>
<td>
// javascript code below that calls the Application/editApp action
<div [email protected]_id.ToString() class="glyphicon glyphicon-pencil btn_edit_app" aria-hidden="true" style="color:blue; cursor:pointer"></div>
</td>
// other fields
<td><div [email protected]_id.ToString() class="js_appname">@app.name</div></td>
// other app fields shown.
</tr>
}
</tbody>
Код Javascript для вызова Application/editApp:
$('.btn_edit_app').click(function () {
var app_to_edit = $(this).attr('id');
$.ajax({
url: '/Application/editApp',
contentType: 'application/html; charset=utf-8',
data: { app_id: app_to_edit },
type: 'GET',
dataType: 'html',
success: function (result) {
// dump the result in a dialog box with a tabbed div.
$('#div_editapp_dialog').html(result);
$('#edit_tabs').tabs({ active: 0 });
$('#edit_dialog').dialog({ width: 700, height: 400 });
}
});
});
Действие приложения/редактировать приложение:
[HttpGet]
public PartialViewResult editApp(int app_id)
{
// get the app based on the app_id
return PartialView("_EditApplication", app);
}
Я могу просмотреть форму с вкладками после выполнения приведенного выше кода, и она работает хорошо.
У меня проблема с проверкой модели. Когда я иду, чтобы сохранить форму, я проверяю модель, и в случае сбоя я хочу, чтобы она вернулась к той же форме с полями. Что было бы самым простым/эффективным способом сделать это?
Приложение/saveApps
[HttpPost]
public ActionResult saveApps (Application app)
{
if (ModelState.IsValid) {
db_context.applications.Add(app);
db_context.SaveChanges();
return RedirectToAction("showApps", "Application");
}
// model validation failed, call the editApp action w/app_id
return RedirectToAction("editApp", "Application", new { app_id = app.app_id });
}
Моя последняя переадресация не работает. Я предполагаю, что вызовов JS для открытия диалогового окна в _EditApplication нет.
return View("_EditApplication",app);
, предполагая, что у вас есть помощники проверки в частичном представлении. - person Shyju   schedule 18.11.2017