我对ASP.NET和MVC还是很陌生,目前正在尝试以下操作:
使用普通的html我可以使用它。到目前为止,我使用剃刀语法无法在表单提交后重新显示这些值。
我的模特:
namespace MyModels
{
public class SubmitTicketFormModel
{
[DisplayName("First Name")]
public string _firstName { get; set; }
[DisplayName("Last Name")]
public string _lastName { get; set; }
}
}
我的观点:
@model MyModels.SubmitTicketFormModel
@{
ViewData["Title"] = "SubmitTicketView";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Request</h1>
@using (Html.BeginForm("SubmitTicketAction", "SubmitTicketContoller", FormMethod.Post))
{
<div class="form-group">
@Html.LabelFor(model => model._firstName)
@Html.TextBoxFor(model => model._firstName, new { @class = "form-control" })
@Html.LabelFor(model => model._lastName)
@Html.TextBoxFor(model => model._lastName, new { @class = "form-control" })
</div>
<input type="submit" value="Post comment" />
}
<table class="table table-bordered table-sm">
<thead class="thead-light">
<tr>
<th>col1</th>
<th>col2</th>
</tr>
</thead>
<tbody>
<tr>
<td>
@Model._firstName
</td>
<td>
@Model._lastName
</td>
</tr>
</tbody>
</table>
控制器:
public class SubmitTicketController : Controller
{
public ActionResult SubmitTicketView()
{
var TicketInstance = new SubmitTicketFormModel();
return View(TicketInstance);
}
[HttpPost]
public ActionResult SubmitTicketAction(SubmitTicketFormModel model)
{
var NewTicketInstance = new SubmitTicketFormModel()
{
_firstName = model._firstName,
_lastName = model._lastName
};
return View(NewTicketInstance);
}
}
}
你能指导我正确的方向吗?
答案 0 :(得分:2)
如果您希望在用户单击“提交”按钮后呈现相同的视图,那么我想您不希望该@using(Html.BeginForm(“ SubmitTicketAction”,“ SubmitTicketContoller”,FormMethod.Post)在用户界面再次显示。在您的视图中,只有名字和姓氏的值已经在视图中写下了逻辑。 在这种情况下,您只需通过控制器在视图中传递一个ViewBag,这将帮助您的View理解它是显示输入表单还是显示用户输入的数据。
[HttpPost]
public ActionResult SubmitTicketAction(SubmitTicketFormModel model)
{
var NewTicketInstance = new SubmitTicketFormModel()
{
_firstName = model._firstName,
_lastName = model._lastName
};
ViewBag.Check = "true";
return View(ViewName , modelname);
}
然后在您看来
@model MyModels.SubmitTicketFormModel
@{
ViewData["Title"] = "SubmitTicketView";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@if(ViewBag.Check != null)
{
<h1>Request</h1>
@using (Html.BeginForm("SubmitTicketAction", "SubmitTicketContoller", FormMethod.Post))
{
<div class="form-group">
@Html.LabelFor(model => model._firstName)
@Html.TextBoxFor(model => model._firstName, new { @class = "form-control" })
@Html.LabelFor(model => model._lastName)
@Html.TextBoxFor(model => model._lastName, new { @class = "form-control" })
</div>
<input type="submit" value="Post comment" />
}
}
else
{
<table class="table table-bordered table-sm">
<thead class="thead-light">
<tr>
<th>col1</th>
<th>col2</th>
</tr>
</thead>
<tbody>
<tr>
<td>
@Model._firstName
</td>
<td>
@Model._lastName
</td>
</tr>
</tbody>
</table>
}