如何检查id为的输入是否为空。ASP.NETMVC
我想检查id为的输入是否为空。 这是我的看法如何检查id为的输入是否为空。ASP.NETMVC,asp.net,asp.net-mvc,asp.net-mvc-4,razor,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,我想检查id为的输入是否为空。 这是我的看法 <div class="form-group form-group-inline"> @Html.TextBoxFor(model => model.DocName, new { id = "DocName1", name ="DocName1"}) </div> <div class="form-group form-group-inline"> @Html.TextBoxFor(mod
<div class="form-group form-group-inline">
@Html.TextBoxFor(model => model.DocName, new { id = "DocName1", name ="DocName1"})
</div>
<div class="form-group form-group-inline">
@Html.TextBoxFor(model => model.DocName, new { id = "DocName2", name ="DocName2"})
</div>
控制器为空
但我想要这样的东西:
id Name
1 DocName1.Value
2 DocName2.Value
var DocumentDb = new Document();
if (!(string.IsNullOrEmpty(model.DocName1)))
db.Documents.Add(DocumentDb);
db.SaveChanges();
{
DocumentDb.Name = model.DocName1;
}
if (!(string.IsNullOrEmpty(model.DocName2)))
{
DocumentDb.Name = model.DocName2;
db.Documents.Add(DocumentDb);
db.SaveChanges();
}
public ActionResult MyController(List<string> docName, int documentID) {
foreach(string doc in docName) {
// do whatever you like
}
}
我的模特
public class Document
{
[Key]
public int DocumentID { get; set; }
public string DocName { get; set; }
}
很抱歉,但我认为您必须更改视图模型,而不是使用此(我认为): 您必须使用以下选项:
public class MyModel {
[Required]
public String DocName1 { get; set; }
[Required]
public String DocName2 { get; set; }
}
他认为:
<div class="form-group form-group-inline">
@Html.TextBoxFor(model => model.DocName1, new { id = "DocName1", name ="DocName1"})
</div>
<div class="form-group form-group-inline">
@Html.TextBoxFor(model => model.DocName2, new { id = "DocName2", name ="DocName2"})
</div>
实例化对象时出错:
执行此操作时:
var DocumentDb = new Document();
从技术上讲,您可以创建一个新的对象指针
执行此操作时,在第一次“保存更改”之后,DocumentDb.DocName=model.DocName2,
实体框架检测到您正在“更新”现有对象(因为指针是相同的)
希望这能有所帮助 将
Model.DocName
作为列表发送给控制器。参见Scott Hanselman的解释
FWIW–如果您提交的是简单的
将类型发送到服务器。也就是说,如果您的请求包含
key=foo&key=bar&key=baz,我们将正确地将其绑定到
IEnumerable
,IList
,ICollection
,T[]
,Collection
,或
列表
。在本段第一句中,“简单类型”指
一种
TypeDescriptor.GetConverter(typeof(T)).CanConvertFrom(typeof(string))
返回true。这使得少数情况更简单
将控制器设置为如下所示:
id Name
1 DocName1.Value
2 DocName2.Value
var DocumentDb = new Document();
if (!(string.IsNullOrEmpty(model.DocName1)))
db.Documents.Add(DocumentDb);
db.SaveChanges();
{
DocumentDb.Name = model.DocName1;
}
if (!(string.IsNullOrEmpty(model.DocName2)))
{
DocumentDb.Name = model.DocName2;
db.Documents.Add(DocumentDb);
db.SaveChanges();
}
public ActionResult MyController(List<string> docName, int documentID) {
foreach(string doc in docName) {
// do whatever you like
}
}
public ActionResult MyController(列表docName,int documentID){
foreach(docName中的字符串doc){
//你想干什么就干什么
}
}
因为我的英语,我想我无法解释我的问题。很抱歉我希望它保持在数据库中,如上面(更新)你的意思是什么?您需要知道如何在数据库中存储这两个字段?在您清楚地解释了这个问题之后,我认为@jao answer是正确的。我在考虑一个有两个特定字段的模型。但是您需要一个字符串列表,并且应该使用数组。对于第二个问题(数据库),请注意实体框架和对象指针。您能给我们看一下您的控制器代码吗?我已经更新了我的问题。我只想检查id=“DocName1”和id=“DocName2”的输入是否为空