ASP.NET Core ViewData,BindProperty还是TempData?

时间:2018-08-28 21:37:36

标签: asp.net-core razor-pages

在ASP.NET Core视图和Razor页面中,我们可以使用:

public class LoginModel
{
    [BindProperty]
    public bool DisplayCaptcha { get; set; }

    // OR

    [ViewData]
    public bool DisplayCaptcha { get; set; }

    // OR

    [TempData]
    public bool DisplayCaptcha { get; set; }
}

在视图/页面/控制器之间共享数据... 但是什么时候使用每个?

在我的情况下,它是一个简单的登录页面,当用户设置了错误的密码时,我将显示一个验证码。

在表单发布中,我将属性设置为true(DisplayCaptcha = true),并使用验证码呈现页面:

@if (Model.DisplayCaptcha)
{            
    <div class="captcha-header">
        ...
    </div>
}

这工作正常,但是我很少混淆属性应该是什么类型,或者即使我应该使用任何类型。

2 个答案:

答案 0 :(得分:3)

将数据从PageModel传递到Page时,应使用

import Cocoa import IOBluetooth import PlaygroundSupport class BlueDelegate : IOBluetoothDeviceInquiryDelegate { func deviceInquiryStarted(_ sender: IOBluetoothDeviceInquiry) { print("Inquiry Started...") //optional, but can notify you when the inquiry has started. } func deviceInquiryDeviceFound(_ sender: IOBluetoothDeviceInquiry, device: IOBluetoothDevice) { print("\(device.addressString!)") print("\(device.name!)") print("\(device.services!)") } func deviceInquiryComplete(_ sender: IOBluetoothDeviceInquiry!, error: IOReturn, aborted: Bool) { //optional, but can notify you once the inquiry is completed. } } var delegate = BlueDelegate() var ibdi = IOBluetoothDeviceInquiry(delegate: delegate) ibdi?.updateNewDeviceNames = true ibdi?.start() PlaygroundPage.current.needsIndefiniteExecution = true 20-12-dc-fb-cf-ab AB Shutter 3 [Address: 0x7fdd5745de60 ServiceName: Bluetooth Wireless Keyboard L2CAP PSM: 17 Attributes: { 0 = "uint32(65536)"; 522 = "bool8(TRUE)"; 519 = "{ { uint16(1033), uint16(256) } }"; 5 = "{ uuid16(10 02) }"; 527 = "uint16(792)"; 516 = "bool8(TRUE)"; 13 = "{ { { uuid16(01 00), uint16(19) }, { uuid16(00 11) } } }"; 524 = "uint16(3200)"; 513 = "uint16(273)"; 256 = "string( Bluetooth Wireless Keyboard )"; 521 = "bool8(TRUE)"; 518 = "{ { uint8(34), string(\t\U00a1\U0085u\U0095\b\a\U00e0)\U00e7%\U0081\U0095u\b\U0081\U0095u\b)\U0091\U0095u\U0091\U0095u\b&\U00ff\a)\U00ff\U0081\U00c0\f\t\U00a1\U0085%u\U0095\n#\n!\n\U008a\n\U0096\n\U009e\n\n\n\n\n\a\n\b\n\n\n\v\n\U00ae\n\U00b1\t@\t0\t\U00b7\t\U00b6\t\U00cd\t\U00b5\t\U00e2\t\U00ea\t\U00e9\U0081\U00c0\f\t\U00a1\U0085\t\U00a1\t &\U00ffu\b\U0095\U0081\U00c0\U00c0\t\U0080\U00a1\U0085%u\U0095\t\U0082\U0081\U0095u\a\U0081\U00c0\f\t\U00a1\U0085%u\U0095\U0081u\U0095\f\t\U00b8\U0081\U00ff\t\U0081u\U0095\U0081\U00c0\f\t\U00a1\U0085\U00ff\U0095u$)&\U0081u\U0081\U00c0) } }"; 4 = "{ { uuid16(01 00), uint16(17) }, { uuid16(00 11) } }"; 526 = "bool8(TRUE)"; 515 = "uint8(33)"; 258 = "string( )"; 1 = "{ uuid16(11 24) }"; 523 = "uint16(256)"; 512 = "uint16(256)"; 9 = "{ { uuid16(11 24), uint16(256) } }"; 6 = "{ uint16(25966), uint16(106), uint16(256) }"; 528 = "uint16(0)"; 517 = "bool8(TRUE)"; 525 = "bool8(FALSE)"; 514 = "uint8(64)"; 257 = "string(Keyboard)"; } , Address: 0x7fdd5742bfa0 ServiceName: (null) L2CAP PSM: 1 Attributes: { 0 = "uint32(65537)"; 9 = "{ { uuid16(12 00), uint16(256) } }"; 512 = "uint16(259)"; 1 = "{ uuid16(12 00) }"; 516 = "bool8(TRUE)"; 513 = "uint16(4933)"; 517 = "uint16(2)"; 514 = "uint16(28705)"; 515 = "uint16(4098)"; 4 = "{ { uuid16(01 00), uint16(1) }, { uuid16(00 01) } }"; } ]

通过POST / GET将数据从PageModel传递到Page时,反之亦然,应使用

ViewData。这是双向绑定。

BindProperty应该在数据只能读取一次时使用。

您应使用TempData

答案 1 :(得分:0)

我们使用ViewData和TempData在回发期间维护对象的状态。但是因为您在每次回发中都设置了它的值,并且它是模型的一部分,所以它应该是[BindProperty]。