我是MVC的新手,但我很熟悉在WebForms工作。我有一个关于何时进行回发的问题。下面是我的模型,视图和控制器:
型号:
public class CategoriesViewModels
{
[Display(Name = "ID")]
public long ID { get; set; }
[Required]
[Display(Name = "Nombre")]
public string NAME { get; set; }
[Display(Name = "Sube imagen para la categoría")]
public string PICTURE { get; set; }
public string PATHPICTURE { get{ return PathFiles.Avatar + PICTURE; } }
public List<SubCategoriesViewModels> SUBCATEGORY { get; set; }
[Display(Name = "Nombre")]
public SubCategoriesViewModels NEW_SUBCATEGORY { get; set; }
public bool? ResultCorrect { get; set; }
public string ResultMessage { get; set; }
public bool? ResultCorrect_Subcategory { get; set; }
public string ResultMessage_Subcategory { get; set; }
public CategoriesViewModels()
{
SUBCATEGORY = new List<SubCategoriesViewModels>();
ResultCorrect = null;
ResultCorrect_Subcategory = null;
}
}
public class SubCategoriesViewModels
{
[Display(Name = "ID")]
public long ID { get; set; }
[Required]
[Display(Name = "Nombre")]
public string NAME { get; set; }
}
查看:
@model Q2BTeamWeb.Models.CategoriesViewModels
@{
ViewBag.Title = "Detail";
}
@using (Html.BeginForm("Detail", "Categories", FormMethod.Post, new { name = "categories-form", id = "categories-form", enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
if (Model.PICTURE != null)
{
@Html.HiddenFor(m => m.PICTURE)
}
<div class="panel form-horizontal">
<div class="panel-heading">
<div class="row">
<div class="col-md-12">
<h4>Detalle categoría</h4>
<a href="@Url.Action("List", "Categories")" class="btn btn-danger pull-right">Volver</a>
</div>
</div>
</div>
</div>
<div class="panel form-horizontal">
<div class="panel-body">
<div class="row">
<div class="col-md-12">
<div class="col-md-2">
@if (Model.PICTURE != "" && Model.PICTURE != null)
{
<img src="@Url.Content(Model.PATHPICTURE)" style=" width: 100%;" />
}
</div>
<div class="col-md-10">
<div class="col-sm-4">
<div class="form-group no-margin-hr">
@Html.LabelFor(m => m.PICTURE, new { @class = "control-label" })
<input type="file" name="upFoto" id="upFoto" class="form-control" />
</div>
</div>
</div>
<div class="col-md-10 col-md-offset-2">
<div class="col-md-4">
@Html.LabelFor(m => m.NAME, new { @class = "control-label" })
@Html.TextBoxFor(m => m.NAME, new
{
@class = "form-control",
placeholder = @Html.DisplayNameFor(m => m.NAME), required = "Obligatorio"
})
</div>
</div>
</div>
</div>
</div>
</div>
<div class="panel form-horizontal">
<div class="panel-footer">
<div class="row">
<div class="col-md-12">
@if (Model.ResultCorrect != null && Model.ResultCorrect.Value)
{
if (Model.ResultCorrect == true)
{
<label style="color:green;font-size:16px;">
@Model.ResultMessage
</label>
}
else
{
<label style="color:red;font-size:16px;">
@Model.ResultMessage
</label>
}
}
<input type="submit" value="@(Model.ID == 0 ? "Crear" : "Actualizar")" class="btn btn-primary pull-right" onclick="return checkForm();" name="createCategory" />
</div>
</div>
</div>
</div>
}
@using (Html.BeginForm("ListSubcategories", "Categories", FormMethod.Post, new { name = "categories-form", id = "categories-form", enctype = "multipart/form-data" }))
{
<div class="panel form-horizontal">
<div class="panel-heading">
<div class="row">
<div class="col-md-12">
<h4>Listado subcategorías</h4>
</div>
</div>
</div>
</div>
<div class="panel form-horizontal">
<div class="panel-body">
<div class="row">
<div class="col-md-12">
@if (Model.SUBCATEGORY != null)
{
<table class="table table-striped" id="subcategories">
<tr>
<th>
Nombre
</th>
<th></th>
</tr>
@foreach (var subcategory in Model.SUBCATEGORY)
{
<tr>
<td>
@Html.DisplayFor(modelItem => subcategory.NAME)
</td>
<td>
<span class="pull-right">
<a href="@Url.Action("Detail", "Categories", new { id = subcategory.ID })" title="Editar"><i class="fa fa-search fa-lg"></i></a>
</span>
</td>
</tr>
}
</table>
}
</div>
</div>
</div>
</div>
<div class="panel form-horizontal">
<div class="panel-heading">
<div class="row">
<div class="col-md-12">
<h4>Nueva subcategoría</h4>
</div>
</div>
</div>
</div>
<div class="panel form-horizontal">
<div class="panel-body">
<div class="row">
<div class="col-md-12">
<div class="col-md-4">
@Html.LabelFor(m => m.NEW_SUBCATEGORY, new { @class = "control-label" })
@Html.TextBoxFor(m => m.NEW_SUBCATEGORY, new
{
@class = "form-control",
placeholder = @Html.DisplayNameFor(m => m.NEW_SUBCATEGORY),
required = "Obligatorio",
})
</div>
</div>
</div>
</div>
</div>
<div class="panel form-horizontal">
<div class="panel-footer">
<div class="row">
<div class="col-md-12">
@if (Model.ResultCorrect_Subcategory != null && Model.ResultCorrect_Subcategory.Value)
{
if (Model.ResultCorrect == true)
{
<label style="color:green;font-size:16px;">
@Model.ResultMessage_Subcategory
</label>
}
else
{
<label style="color:red;font-size:16px;">
@Model.ResultMessage_Subcategory
</label>
}
}
<input type="submit" value="Crear" class="btn btn-primary pull-right" onclick="return checkForm();" name="createSubcategory" />
</div>
</div>
</div>
</div>
}
控制器:
public CategoriesController() { }
public ActionResult List()
{
List<CategoriesViewModels> lCategories = new List<CategoriesViewModels>();
foreach (DataRow r in CategoriesManagement.list(null).Rows)
{
CategoriesViewModels c = new CategoriesViewModels();
c.ID = long.Parse(r["ID"].ToString());
c.NAME = r["NAME"].ToString();
lCategories.Add(c);
}
return View(lCategories);
}
public ActionResult Detail()
{
string __id = (string)this.RouteData.Values["id"];
string create_ok = Request.QueryString["create"];
CategoriesViewModels Category = nenter code hereew CategoriesViewModels();
if (__id != null)
{
foreach (DataRow r in CategoriesManagement.list(long.Parse(__id)).Rows)
{
Category.ID = long.Parse(r["ID"].ToString());
Category.NAME = r["NAME"].ToString();
Category.PICTURE = r["PICTURE"].ToString();
foreach (DataRow r2 in SubCategoriesManagement.list(categoriesId: long.Parse(__id)).Rows)
{
SubCategoriesViewModels s = new SubCategoriesViewModels();
s.ID = long.Parse(r2["ID"].ToString());
s.NAME = r2["NAME"].ToString();
Category.SUBCATEGORY.Add(s);
}
}
}
if (create_ok == "ok")
{
Category.ResultMessage = "Creado correctamente";
Category.ResultCorrect = true;
}
return View(Category);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Detail(CategoriesViewModels c)
{
if (Request.Params["createCategory"] != null)
{
HttpPostedFileBase photo = Request.Files["upFoto"];
string directory = @Server.MapPath("~/Images/Categories");
if (photo != null && photo.ContentLength > 0)
{
var fileName = Path.GetFileName(photo.FileName);
photo.SaveAs(Path.Combine(directory, fileName));
c.PICTURE = fileName;
}
try
{
long id_return = c.ID;
c.ResultCorrect = true;
CategoriesManagement.action(ref id_return, c.NAME, c.PICTURE);
if (c.ID == 0)
{
return RedirectToAction("Detail", "Categories", new { id = id_return, create = "ok" });
}
else
c.ResultMessage = "Actualizado correctamente";
return View(c);
}
catch (Exception ex)
{
c.ResultCorrect = false;
c.ResultMessage = "Error " + ex.Message;
}
}
return View(c);
}
我的问题是当我从第一个提交回发时,我在列表底部的数据库表中进行更新,然后我必须回发丢失的值。
我该怎么做才能防止这种情况发生?
在WebForms中,我有一个gridview,这些数据存储在内存中,不像MVC那样丢失。我是否需要返回查询以避免丢失数据?或者是?