html 表单控制器,.NET Core 3.1:将值从CSHTML页面输入表单字段传递给控制器

我正在寻找一种将值从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的新手,这里编写的大多数代码不是我自己编写的,而是由我的舞台主管分配的,因此,如果可能的话,不要只向我扔一些代码:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值