Asp.net mvc ASP.NET MVC使用表单数据输入创建xml

Asp.net mvc ASP.NET MVC使用表单数据输入创建xml,asp.net-mvc,Asp.net Mvc,我有一个包含多个文本框、复选框和提交按钮的视图。单击submit时,需要将数据传递给DataController,DataController将创建模型对象并将对象序列化为xml。我应该如何将所有这些数据传递给控制器?这就是我现在所拥有的 Data.cshtml <legend>Enter Categories</legend> <div class="row"> <div class="col-md-2 col-md-pull">

我有一个包含多个文本框、复选框和提交按钮的视图。单击submit时,需要将数据传递给DataController,DataController将创建模型对象并将对象序列化为xml。我应该如何将所有这些数据传递给控制器?这就是我现在所拥有的

Data.cshtml

<legend>Enter Categories</legend>   
<div class="row">
    <div class="col-md-2 col-md-pull">
        Name:
    </div>
    <div class="col-md-8 col-md-push">
        <input id="NameTextBox" type="text" placeholder="Enter a name ..." class="form-control" required="required" autofocus="autofocus" />
    </div>
</div>

<br />

<div class="row event-selection">
    <div class="col-md-3">
        <input id="Data1" type="text" class="form-control" placeholder="FirstName1" />
    </div>
    <div class="col-md-3">
        <input id="Data2" type="text" class="form-control" placeholder="LastName1" />
    </div>
    <div class="col-md-3">
        <input id="Data3" type="text" class="form-control" placeholder="FirstName2" />
    </div>
    <div class="col-md-3">
        <input id="Data4" type="text" class="form-control" placeholder="LastName2" />
    </div>
</div>

<br />

<div class="row">
    <button class="btn"><img src="~/Content/Images/Add.PNG" /></button>
    Add new category
</div>

<br />
<br />

<legend>Select Class</legend>
<div>
    <label class="checkbox-inline">
        <input type="checkbox" />
        Primary
    </label>
    <label class="checkbox-inline">
        <input type="checkbox" />
        Secondary
    </label>
    <label class="checkbox-inline">
        <input type="checkbox" />
        High
    </label>
    <label class="checkbox-inline">
        <input type="checkbox" />
        Low
    </label>
    <label class="checkbox-inline">
        <input type="checkbox" />
        B
    </label>
    <label class="checkbox-inline">
        <input type="checkbox" />
        G
    </label>
    <label class="checkbox-inline">
        <input type="checkbox" />
        P
    </label>
</div>

<br />
<br />

<legend>Select Area</legend>
<select class="dropdown" id="AreaDropDown">
    <option class="dropdown-header">Select a area ...</option>
</select>

<br />
<br />

<legend>Download</legend>
<input type="submit" class="btn btn-primary" value="Download Data" onclick="location.href='@Url.Action("Download", "Data")'" />

创建一个附加对象/视图模型,并将其用于表单/控制器映射。然后,您可以填充各种XML对象,您需要将
name
属性分配给各种表单标记以完成映射

public class MyViewModel
{
    public Name Name { get; set; }
    ...
}

public class DataController : Controller
{
    [HttpPost]
    public ActionResult Download(MyViewModel myViewModel)
    {
        ...
    }
}

<legend>Enter Categories</legend>   
<div class="row">
    <div class="col-md-2 col-md-pull">
        Name:
    </div>
    <div class="col-md-8 col-md-push">
        <input id="NameTextBox" type="text" name="Name" ... />
    </div>
</div>
公共类MyViewModel
{
公共名称名称{get;set;}
...
}
公共类DataController:控制器
{
[HttpPost]
公共操作结果下载(MyViewModel MyViewModel)
{
...
}
}
输入类别
姓名:

您的问题不清楚您是否需要其他帮助来创建XML或只是表单/控制器映射?

这是我寻求帮助的第一个领域。我对生成xml有一些想法,但我将就此发表一个单独的问题。我尝试了你的解决方案,现在当我点击提交按钮时,我得到了404,因为现在数据/下载不存在。我调用onclick=“location.href=”@Url.Action(“下载”、“数据”)的onclick事件“。看来MyViewModel对象也需要传入?我了解到我不能使用location.href发布表单数据,这是我在提交按钮上使用的。还有什么替代方法?是的,
location.href
会导致一个
GET
请求。相反,你需要用
标记.Y包装表单元素您可以使用MVC帮助程序来设置格式,但它们不是必需的。您也可以使用jQuery post()方法,但这可能会使问题变得复杂。
public class DataModel
{
    [XmlRoot("Root")]
    public class Person
    {
        [XmlElement("Name")]
        public Name Name { get; set; }
    }

    [SerializableAttribute()]
    public class Name
    {
        [XmlElement("FirstName")]
        public string FirstName { get; set; }
        [XmlElement("LastName")]
        public string LastName { get; set; }
    }
...
}
public class MyViewModel
{
    public Name Name { get; set; }
    ...
}

public class DataController : Controller
{
    [HttpPost]
    public ActionResult Download(MyViewModel myViewModel)
    {
        ...
    }
}

<legend>Enter Categories</legend>   
<div class="row">
    <div class="col-md-2 col-md-pull">
        Name:
    </div>
    <div class="col-md-8 col-md-push">
        <input id="NameTextBox" type="text" name="Name" ... />
    </div>
</div>