如何ID值从操作方法进入到视图 [英] How value of id goes from action method to the view
问题描述
我有一个方法 AddAddress(INT编号)
我不能工作了id的值是如何去查看(当我检查视图源
渲染视图的价值是存在的)。
我要创建更复杂,但类似的方法,我只是不明白编号
的价值如何去渲染视图。我只需要知道它是如何发生的。非常感谢
AddAddress方式:
[HTTPGET]
公众的ActionResult AddAddress(INT标识)
{
返回查看();
}[HttpPost]
公共IActionResult AddAddress(AddressModel地址)
{
如果(!ModelState.IsValid)
{
返回视图(AddAddress,地址);
}
如果(!address.Address_Id.HasValue || address.Address_Id == 0)
{
_addressService.insertAddress(地址);
}
其他
{
_addressService.updateAddress(地址);
}
返回RedirectToAction(「指数」);
}
接收标识视图:
@model Clients.Models.AddressModel@using(Html.BeginForm(FormMethod.Post))
{
@ Html.ValidationSummary() @ Html.HiddenFor(M = GT; m.Id)LT; BR />
@ Html.HiddenFor(M = GT; m.Address_Id)LT; BR />
@ Html.LabelFor(M = GT; m.AddressLine_1)
@ Html.TextBoxFor(M = GT; m.AddressLine_1)
@ Html.ValidationMessageFor(M = GT; m.AddressLine_1)LT; BR />
@ Html.LabelFor(M = GT; m.AddressLine_2)
@ Html.TextBoxFor(M = GT; m.AddressLine_2)
@ Html.ValidationMessageFor(M = GT; m.AddressLine_2)LT; BR />
@ Html.LabelFor(M = GT; m.Post code)
@ Html.TextBoxFor(M = GT; m.Post code)
@ Html.ValidationMessageFor(M = GT; m.Post code)< BR />
@ Html.LabelFor(M = GT; m.Town)
@ Html.TextBoxFor(M = GT; m.Town)
@ Html.ValidationMessageFor(M = GT; m.Town)LT; BR />
@ Html.LabelFor(M = GT; m.DateMovedIn)
@ Html.TextBoxFor(M = GT; m.DateMovedIn)
@ Html.ValidationMessageFor(M = GT; m.DateMovedIn)LT; BR /> <输入类型=提交VALUE =OK/>
}
您的Razor视图是强类型为 AddressModel
类。因此,在您的GET操作方法,创建 AddressModel
类的一个对象,将编号
属性值设置为任何值来动作方法的参数,并将其发送对象的视图。
[HTTPGET]
公众的ActionResult AddAddress(INT标识)
{
VAR模型=新AddressModel();
model.Id = ID;
返回查看(模型);
}
您需要明确数据从操作方法传递给你的看法。有没有其他的神奇方式来做到这一点。如果你不这样做,它仍然会具有整数类型属性的默认值,即0。
I have a method AddAddress(int Id)
and I cannot work out how the value of Id goes to the View (when I check view source
of the rendered view the value is there).
I have to create more complicated but similar method and i just don't get how the value of Id
goes to the rendered View. I just need to know how it happens. Many thanks
AddAddress Method :
[HttpGet]
public ActionResult AddAddress (int Id)
{
return View();
}
[HttpPost]
public IActionResult AddAddress(AddressModel address)
{
if (!ModelState.IsValid)
{
return View("AddAddress", address);
}
if (!address.Address_Id.HasValue|| address.Address_Id == 0)
{
_addressService.insertAddress(address);
}
else
{
_addressService.updateAddress(address);
}
return RedirectToAction("Index");
}
the view that receives Id:
@model Clients.Models.AddressModel
@using (Html.BeginForm(FormMethod.Post))
{
@Html.ValidationSummary()
@Html.HiddenFor(m => m.Id)<br />
@Html.HiddenFor(m => m.Address_Id)<br />
@Html.LabelFor(m => m.AddressLine_1)
@Html.TextBoxFor(m => m.AddressLine_1)
@Html.ValidationMessageFor(m => m.AddressLine_1)<br />
@Html.LabelFor(m => m.AddressLine_2)
@Html.TextBoxFor(m => m.AddressLine_2)
@Html.ValidationMessageFor(m => m.AddressLine_2)<br />
@Html.LabelFor(m => m.Postcode)
@Html.TextBoxFor(m => m.Postcode)
@Html.ValidationMessageFor(m => m.Postcode)<br />
@Html.LabelFor(m => m.Town)
@Html.TextBoxFor(m => m.Town)
@Html.ValidationMessageFor(m => m.Town)<br />
@Html.LabelFor(m => m.DateMovedIn)
@Html.TextBoxFor(m => m.DateMovedIn)
@Html.ValidationMessageFor(m => m.DateMovedIn)<br />
<input type="submit" value="OK"/>
}
Your razor view is strongly typed to AddressModel
class. So in your GET action method, create an object of AddressModel
class, set the Id
property value to whatever value came to your action method as parameter and send this object to the view.
[HttpGet]
public ActionResult AddAddress (int Id)
{
var model = new AddressModel();
model.Id = id;
return View(model);
}
You need to explicitly pass data to your view from your action method. There is no other magic way to do that. If you do not do that, It will still have the default value of integer type properties, which is 0.
这篇关于如何ID值从操作方法进入到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!