0

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

数据.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")'" />

数据控制器.cs

public class DataController : Controller
{
    [HttpPost]
    public ActionResult Download() // How should the form data get passed into this?
    {

        // Create objects using form data passed

        return new XmlResult<DataModel.XmlData>()
        {
            Data = dataModelInstance
        };
    }
}

数据模型.cs

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

1 回答 1

0

创建一个附加对象/视图模型并将其用于您的表单/控制器映射。然后,您可以填充各种 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>

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

于 2013-10-30T20:21:34.153 回答