我正在寻找一种将值从Razor页面传递到控制器的方法。
这些是我的模型:
public class DocumentModel
{
public int Id { get; set; }
[Display(Name = "Titolo")]
[Required, MinLength(3)]
public string Title { get; set; }
[Range(1, int.MaxValue)]
[Display(Name = "Versione Attiva")]
public int ActiveVersion { get; set; }
[Range(1, int.MaxValue)]
[Display(Name = "Ultima Versione")]
public int LastVersion { get; set; }
public virtual ICollection DocumentVersion { get; set; }
}
public class DocumentVersionModel
{
public int Id { get; set; }
public int DocumentModelId { get; set; }
[DataType(DataType.Date, ErrorMessage="Date only")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime CreationDate { get; set; }
[DataType(DataType.Date, ErrorMessage="Date only")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime LastChangeDate { get; set; }
[DataType(DataType.Date, ErrorMessage="Date only")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime DocumentDate { get; set; }
public string Content { get; set; }
public int Version { get; set; }
public DocumentModel Document { get; set; }
}
这是我的Create.cshtml页面中的表单:
Titolo
Data del Documento
Versione
Ultima Versione
这是我的Edit.cshtml页面中的表单:
Titolo
Data Documento
//PROBLEM HERE: the DocumentDate value is the today date instead of the chosen date in the input form of the Create.cshtml page
Data Creazione
Data Ultima Modifica
Ultima versione
Versione attiva
@Html.DropDownList("ActiveVersion", ViewBag.versionlist as SelectList, new { id = "ActiveVersionSave", @class = "form-control" })
Versione selezionata
@Html.DropDownList("donotuse", ViewBag.versionlist as SelectList, new { id = "ActiveVersionSelect", @class = "form-control" })
这是我的控制器的两种方法:
// POST: DocumentView/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task Create(DocumentModel documentModel)
{
if (ModelState.IsValid)
{
_context.Add(documentModel);
await _context.SaveChangesAsync().ConfigureAwait(false);
var documentId = documentModel.Id;
return RedirectToAction("edit", new { documentid = documentId });
}
return View(documentModel);
}
// GET: DocumentView/Edit/5
public async Task Edit([FromQuery] int documentid)
{
var wtf = await _context.DocumentModel.Include(e => e.DocumentVersion).AsNoTracking().ToListAsync().ConfigureAwait(false);
var documentModel = await _context.DocumentModel.Include(e=>e.DocumentVersion).Where(i=>i.Id==documentid).FirstOrDefaultAsync().ConfigureAwait(false);
if (documentModel.DocumentVersion.Count == 0)
{
documentModel.DocumentVersion.Add(await AddVersion(documentModel, 1).ConfigureAwait(false));
var versionlist = documentModel.DocumentVersion.ToList();
ViewBag.versionlist =new SelectList(versionlist,"Version","Version");
}
else
{
var versionlist = documentModel.DocumentVersion.ToList();
ViewBag.versionlist =new SelectList(versionlist,"Version","Version");
}
return View(documentModel);
}
我想保存“ Data del Documento”的值以使其出现在Edit.cshtml页面中。
注意:我是C#和MVC的新手,这里编写的大多数代码不是我自己编写的,而是由我的舞台主管分配的,因此,如果可能的话,不要只向我扔一些代码:)