[英]C# RazorPg - Check if form field is empty and mark as unchanged
I have a form field that allows users to change their email address.我有一个表单域,允许用户更改他们的 email 地址。 If the user doesn't enter anything in the field I want to send "unchanged" to the API. Not blank "" or null.如果用户未在字段中输入任何内容,我想将“未更改”发送到 API。不是空白“”或 null。
I've came up with this code:我想出了这段代码:
if (!String.IsNullOrEmpty(Request.Form["Email"].ToString())) // Null or blank check
{
if (Request.Form["Email"].ToString() != user.Email) // Don't update email if it's the same as the existing one
{
user.Email = Request.Form["Email"];
}
else
{
user.Email = "unchanged"; // I don't want to pass null or blank to the API.
}
}
else
{
user.Email = "unchanged";
}
It just looks very messy to me.对我来说它看起来很乱。 I have 10 fields on the page so I'd be listing that 10 times in my controller.我在页面上有 10 个字段,所以我会在我的 controller 中列出 10 次。
Is there a nicer way of doing this?有更好的方法吗?
I think you can initialize the Email property in your User model:我认为您可以在用户 model 中初始化 Email 属性:
public string Email { get; set; } = "unchanged";
you can do it also in the default constructor.您也可以在默认构造函数中执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.