English 中文(简体)
Edit行动的新价值
原标题:Nullable value in Edit Action

我的埃德特形式,在挽救结果时存在问题。

观点:

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

模式:

namespace Restauracja.Models
{
     public class pracownikModel 
    {
         public LoginModel LoginModel { get; set; }
         public uzytkownikModel uzytkownikModle { get; set; }
       public pracownikModel() {
           LoginModel = new LoginModel();
           uzytkownikModle = new uzytkownikModel();
       }
    }

    public class LoginModel 
    {
        [Required]
        public string Uzytkownik { get; set; }

        [Required]
        public string Haslo { get; set; }

        public string Konto { get; set; }

    }

    public class uzytkownikModel
    {
        [Required]
        public string imie { get; set; }

        ....

    }
}

主计长:

        [HttpGet]
        public ActionResult Edit(int LoginID)
        {

            pracownikModel prac = new pracownikModel();

            var pr = (from p in baza.Logowanies where LoginID == p.LoginID select p).First();

            prac.LoginModel.Uzytkownik = pr.Login;

            return View(prac);
        }

        [HttpPost]
        public ActionResult Edit(int LoginID, pracownikModel prac)
        {
            var xxx = (from z in baza.Logowanies where LoginID == z.LoginID select z).Single();

            xxx.Login = prac.LoginModel.Uzytkownik;
            baza.SubmitChanges();

            return RedirectToAction("Index", "Foo");

        }

Function with [HttpGet] is working properly, showing result from database. The problem is with second function... prac is null... So this cannot work because I can t write null to the database. I don t know how to solve this problem.

最佳回答

I would recommend you to use only standard ASCII letters and numbers when naming your variables and classes. The default model binder uses those names when trying to bind the properties from the POST request. Also since those property names are used as name and id attribute of the input elements in the HTML you end up with invalid HTML according to the specification which states:

  • ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

因此,您不应使用扩展的ASCII或单编码符号,例如<编码> > 和>。 用相应的ASCII特性取代它们,缺省模型约束器将能够纠正这些数值。


UPDATE:

在研究了样本代码之后,你寄给我。 亲爱的观念

@model Restauracja.Models.pracownikModel

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Edit</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.LoginModel.Uzytkownik)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LoginModel.Uzytkownik)
            @Html.ValidationMessageFor(model => model.LoginModel.Uzytkownik)
        </div>
    </fieldset>
}

<table id="nostyle">
<tr>
    <td>
        @using (Html.BeginForm())
        {
            @Html.ValidationSummary(true)
            <input type="submit" value="Save" />
        }
    </td>
    <td>
        @using (Html.BeginForm("Anuluj", "Pracownik", FormMethod.Post, new { id = "AnulujForm" }))
        {
            <input id="Anuluj" type="submit" value="Cancel" />
        }
    </td>
</tr>
</table>

第一个表格是你模式唯一包含投入领域的形式。 因此,仅提交一种形式,即你可以期望在《经济、社会、文化权利国际公约》行动中把你模式的某些特性集中起来。 不幸的是,第一种形式没有提交纽伦,因此无法提交。 第二类和第三类包含的是提交年份,但并不包含任何投入领域,因此提交这些清单时,模型将完全空白。

So what you need here is to move the Save submit button inside the first form:

@model Restauracja.Models.pracownikModel

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) 
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Edit</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.LoginModel.Uzytkownik)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LoginModel.Uzytkownik)
            @Html.ValidationMessageFor(model => model.LoginModel.Uzytkownik)
        </div>
    </fieldset>

@:<table id="nostyle">
    @:<tr>
        @:<td>
            <input type="submit" value="Save" />
        @:</td>
}
        <td>
            @using (Html.BeginForm("Anuluj", "Pracownik", FormMethod.Post, new { id = "AnulujForm" }))
            {
                <input id="Anuluj" type="submit" value="Cancel" />
            }
        </td>
    </tr>
</table>

or since the Cancel submit button posts currently to a controller action tat performs a redirect you could simply replace it with an anchor which will redirect:

@using (Html.BeginForm()) 
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Edit</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.LoginModel.Uzytkownik)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LoginModel.Uzytkownik)
            @Html.ValidationMessageFor(model => model.LoginModel.Uzytkownik)
        </div>
    </fieldset>

    <table id="nostyle">
        <tr>
            <td>
                <input type="submit" value="Save" />
            </td>
            <td>
                @Html.ActionLink("Cancel", "zarzadzaj_pracownikami", "Pracownik")
            </td>
        </tr>
    </table>
}
问题回答

暂无回答




相关问题
LINQ to SQL as databinding source for WPF Treeview

I wonder if someone could provide a simple example of the following. (preferably in VB.Net): I have an SQL database with related tables and I am successfully using LINQ to SQL in other areas of my ...

Linq to SQL insert/select foreign/primary records

I have table A and Table B. Table B contains two columns, Name and ID. Table A contains several columns and a foreign key column pointing to B.ID called B_ID On the client side, I have all the data I ...

LINQ to SQL Dynamic Sort question

How do I code the Select clause in my LINQ satament to select column aliases so I can sort on them basically I want to accomplish this SQL statement in LINQ: select type_id as id, ...

Using a schema other than dbo in LinqToSql Designer

Is there a way to specify in a data connection or the LinqToSql Designer a schema? Whenever I go to setup a data connection for LinqToSql there doesn t seem to be anyway to specify a schema and I ...