使用只读属性设置输入字段的默认值

时间:2017-09-06 09:45:01

标签: javascript c# html forms default

我有一个将持久数据发布到SQL数据库的表单。在我的表单中,我有一个输入字段,首先设置为只读,除非用户选择一个选项:

enter image description here

我的问题是,如果用户选择不在输入字段中输入值,则会收到此错误:

enter image description here

我尝试在我的javascript中设置输入的默认值,但是我收到了同样的错误。

$("#lejeperiode").val(" ");

将空字符串或null插入到DB中无关紧要。 这是我的输入字段:

<input id="periodeInput" class="form-control" type="text" name="lejeperiode" value="@(Request.Form["lejeperiode"] != null ? Request.Form["lejeperiode"].ToString() : "")"/>

1 个答案:

答案 0 :(得分:1)

修改代码以检查字符串是否为空如果是,“”将是您的默认其他POST值。

cmd.Parameters.AddWithValue("@lejeperiode", string.IsNullOrEmpty(Request.Form["lejeperiode"]) ? "" : Request.Form["lejeperiode"].ToString())