Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC的问题_C#_Asp.net Mvc - Fatal编程技术网

C# MVC的问题

C# MVC的问题,c#,asp.net-mvc,C#,Asp.net Mvc,我是MVC的新生,所以我有一些问题。我想创建表单,在这里我可以插入一些信息,比如国家、地区等,然后将其序列化到文件中。但是我的Create方法get object没有任何信息。那我哪里做错了 [HttpPost] // This is my Method which must Serialize class Address public ActionResult Create(Address address) { if (ModelState.IsValid)

我是MVC的新生,所以我有一些问题。我想创建表单,在这里我可以插入一些信息,比如国家、地区等,然后将其序列化到文件中。但是我的Create方法get object没有任何信息。那我哪里做错了

 [HttpPost] // This is my Method which must Serialize class Address
    public ActionResult Create(Address address)
    {
        if (ModelState.IsValid)
        {
            // Размещаем в XML данные
            System.Xml.Serialization.XmlSerializer writer =
                new System.Xml.Serialization.XmlSerializer(typeof(Address));

            System.IO.StreamWriter file = new System.IO.StreamWriter(@"c:\MvcApp.xml");
            writer.Serialize(file, address);
            file.Close();
            return RedirectToAction("Index");
        }
        return View(address);
    }
这是我的视图文件

 @model WorkWithAddresses.Models.Address

 @{
ViewBag.Title = "Create";
 }

 <h2>Create</h2>

 @using (Html.BeginForm())
 {
@Html.ValidationSummary(true)
    <fieldset>
        <legend>Address</legend>
        <div class="editor-label">
            @Html.LabelFor(model=>model.country, "Country")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model=>model.country)
            @Html.ValidationMessageFor(model=>model.country)
        </div>

        <div class ="editor-label">
            @Html.LabelFor(model=>model.region, "Region")
        <div>
        <div class="editor-field">
            @Html.EditorFor(model=>model.region)
            @Html.ValidationMessageFor(model=>model.region)
        </div>

            <div class ="editor-label">
                @Html.LabelFor(model=>model.locality,"Locality")
            </div>
            <div class ="editor-field">
                    @Html.EditorFor(model=>model.locality)
                    @Html.ValidationMessageFor(model=>model.locality)
            </div>

             <div class ="editor-label">
                @Html.LabelFor(model=>model.locality,"Street")
            </div>
            <div class ="editor-field">
                    @Html.EditorFor(model=>model.street)
                    @Html.ValidationMessageFor(model=>model.street)
            </div>

            <div class ="editor-label">
                @Html.LabelFor(model=>model.locality,"House")
            </div>
            <div class ="editor-field">
                    @Html.EditorFor(model=>model.houseNumber)
                    @Html.ValidationMessageFor(model=>model.houseNumber)
            </div>

            <div class ="editor-label">
                @Html.LabelFor(model=>model.locality,"Building")
            </div>
            <div class ="editor-field">
                    @Html.EditorFor(model=>model.buildingNumber)
                    @Html.ValidationMessageFor(model=>model.buildingNumber)
            </div>

             <div class ="editor-label">
                @Html.LabelFor(model=>model.locality,"Apartment")
            </div>
            <div class ="editor-field">
                    @Html.EditorFor(model=>model.apartmentNumber)
                    @Html.ValidationMessageFor(model=>model.apartmentNumber)
            </div>

            <p>
                <input type="submit" value="Create"/>
            </p>
            </fieldset>
}

<div>
  @Html.ActionLink("Back to List","Index")
</div>

 // This is my class which I want to Serialize after user put information
[XmlRoot("Address")]
public class Address
{
    #region Constructors
    public Address()
    {
    }
    #endregion

    #region Public Member Variables
    [XmlElement("AddressId")]
    public int addressId;              // Id адреса
    [XmlElement("Country")]
    public string country;            // Страна
    [XmlElement("Region")]
    public string region;             // Регион
    [XmlElement("Locality")]
    public string locality;           // Населенный пункт
    [XmlElement("Street")]
    public string street;             // Улица
    [XmlElement("HouseNumber")]
    public string houseNumber;        // Номер дома
    [XmlElement("BuildingNumber")]
    public string buildingNumber;     // Номер корпуса
    [XmlElement("ApartmentNumber")]
    public string apartmentNumber;    // Номер квартиры
    [XmlElement("Description")]
    public Description description;   // Описание адреса
    #endregion
  }
 }
@model WorkWithAddresses.Models.Address
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
地址
@Html.LabelFor(model=>model.country,“country”)
@EditorFor(model=>model.country)
@Html.ValidationMessageFor(model=>model.country)
@LabelFor(model=>model.region,“region”)
@EditorFor(model=>model.region)
@Html.ValidationMessageFor(model=>model.region)
@LabelFor(model=>model.locality,“locality”)
@EditorFor(model=>model.locality)
@Html.ValidationMessageFor(model=>model.locality)
@LabelFor(model=>model.locality,“Street”)
@EditorFor(model=>model.street)
@Html.ValidationMessageFor(model=>model.street)
@LabelFor(model=>model.locality,“House”)
@EditorFor(model=>model.houseNumber)
@Html.ValidationMessageFor(model=>model.houseNumber)
@LabelFor(model=>model.locality,“Building”)
@EditorFor(model=>model.buildingNumber)
@Html.ValidationMessageFor(model=>model.buildingNumber)
@LabelFor(model=>model.locality,“公寓”)
@EditorFor(model=>model.apartmentNumber)
@Html.ValidationMessageFor(model=>model.apartmentNumber)

} @ActionLink(“返回列表”、“索引”) //这是我想在用户输入信息后序列化的类 [XmlRoot(“地址”)] 公共课堂演讲 { #区域构造函数 公共广播 { } #端区 #区域公共成员变量 [XmlElement(“AddressId”)] public int addressId;//IdааПаа [XmlElement(“国家”)] 公共字符串国家;//Сааа [XmlElement(“区域”)] 公共字符串区域;//ПБГиц [XmlElement(“位置”)] 公共字符串位置;// [XmlElement(“街道”)] 公共字符串街;//Уааа [XmlElement(“门牌号”)] 公共字符串门牌号;//ББМБМа [XmlElement(“BuildingNumber”)] 公共字符串buildingNumber;//ББМББПСа [XmlElement(“公寓号”)] 公共字符串单元号;//ББМСаааааа [XmlElement(“描述”)] 公共描述描述;//ППаааааааа #端区 } }
视图和控制器操作代码在我看来都很好。问题将出现在模型中的address类中。Mvc需要具有反序列化webforms post到的属性

public string Country { get; set; }

您应该使用title case for convention来命名这些名称,但如果您将它们保持小写,它仍然可以工作。

在我看来很好。您能否确认
GET
操作方法也称为
Create
?那很重要。。因为没有参数的
Html.BeginForm()
将发布到相同的操作名称。。