将多个表单数据添加到单个数据库列

将多个表单数据添加到单个数据库列

问题描述:

我很难搞清楚如何将2个文本框数据添加到数据库中的单个列。将多个表单数据添加到单个数据库列

样品:

<input name="Name" type="text" /> 
<input name="Address" type="text" /> 

,并放在一列看起来像名称+地址

感谢

+0

数据库中的一列应该只包含**一项信息(数据库设计中的绝对基本实践 - 第一范式)。否则,你的下一个SO问题将是如何解析这两个数据库列之外的部分..... – 2012-02-15 06:22:14

举两个相同的名字输入 ,这样的值将被保存逗号单独

如何将2个文本框数据添加到我的da中的单个列tabase。

您可以使用视图模型。

领域模型:

public class Foo 
{ 
    public string Location { get; set; } 
} 

视图模型:

public class FooViewModel 
{ 
    public string Address { get; set; } 
    public string Name { get; set; } 
} 

查看:

@model FooViewModel 
@using (Html.BeginForm()) 
{ 
    @Html.LabelFor(x => x.Address) 
    @Html.EditorFor(x => x.Address) 

    @Html.LabelFor(x => x.Name) 
    @Html.EditorFor(x => x.Name) 
    <button type="submit">Save</button> 
} 

控制器:

public class SomeController: Controller 
{ 
    public ActionResult Save() 
    { 
     return View(new FooViewModel()); 
    } 

    [HttpPost] 
    public ActionResult Save(FooViewModel model) 
    { 
     Foo foo = new Foo 
     { 
      Location = model.Name + model.Address 
     }; 

     //... save the foo domain model to your database 
     ...  
    } 
} 
+0

这是与复选框相同吗? – paul 2012-02-15 08:30:13

+0

@paul,是的,最好总是使用视图模型。 – 2012-02-15 09:15:02

+0

好的。非常感谢 – paul 2012-02-15 09:17:06