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

时间:2012-02-15 05:37:30

标签: html asp.net-mvc razor

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

样品:

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

并放在一个名为+地址

的列中

由于

2 个答案:

答案 0 :(得分:1)

  

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

您可以使用视图模型。

域名模型:

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
        ...       
    }
}

答案 1 :(得分:0)

为输入指定两个相同的名称 这样值将单独保存为逗号