0

我在使用 ASP.MVC 中的 SelectList 时遇到了一些问题。

这是问题所在:我有一个 Create View 并开始了一个 ViewModel 模型。

页面加载得很好(GET 动词)。但是在发布的时候,发生了一些事情,我的模型被认为是无效的,它不能插入。这是我到目前为止所尝试的。

public class DefinitionFormViewModel
{
    private Repository<Category> categoryRepository = new Repository<Category>();
    public Definition ViewDefinition { get; private set; }
    public SelectList Categories { get; private set; }

    public DefinitionFormViewModel(Definition def)
    {
        ViewDefinition = def;

        // here i wanted to place it directly, like shown in NerdDinner Tutorial
        // new SelectList(categoryRepository.All(),ViewDefinition.Category);


        Categories = new SelectList(categoryRepository.All(), "CategoryId", "CategoryName", ViewDefinition.CategoryId);
    }
}

// pageview which inherits DefinitionFormViewModel
        <div class=editor-field">
            <%= Html.DropDownList("Category",Model.Categories) %>
            <%= Html.ValidationMessageFor(model => Model.ViewDefinition.Category) %>
        </div>

// controller methods
    [Authorize]
    public ActionResult Create()
    {
        Definition definition = new Definition();

        return View(new DefinitionFormViewModel(definition));
    }

    [AcceptVerbs(HttpVerbs.Post), Authorize]
    public ActionResult Create(int id,Definition definition)
    {
        Term term = termRepository.SingleOrDefault(t => t.TermId == id);

        if (term == null)
        {
            return View("NotFound", new NotFoundModel("Termo não encontrado", "Termo não encontrado",
            "Nos desculpe, mas não conseguimos encontrar o termo solicitado.", "Indíce de Termos", "Index", "Term"));
        }
        else
        {
            if (ModelState.IsValid)
            {
                try
                {
                    definition.TermId = term.TermId;
                    definition.ResponsibleUser = User.Identity.Name;

                    UpdateModel(definition);

                    term.Definitions.Add(definition);

                    termRepository.SaveAll();

                    return RedirectToAction("Details", "Term", new { id = term.TermId });
                }
                catch (System.Data.SqlClient.SqlException sqlEx)
                {
                    ModelState.AddModelError("DatabaseError", "Houve um erro na inserção desta nova definição");
                }
                catch
                {
                    foreach (RuleViolation rv in definition.GetRuleViolations())
                    {
                        ModelState.AddModelError(rv.PropertyName, rv.ErrorMessage);
                    }
                }
            }
        }
        return View(new DefinitionFormViewModel(definition));
    }

我很抱歉这篇文章很长,但我无法弄清楚。我没有任何图形错误或异常。我的执行在 if (ModelState.IsValid) 中终止。

谢谢你的时间

乔治

4

1 回答 1

0

This should change from

[AcceptVerbs(HttpVerbs.Post), Authorize]
    public ActionResult Create(int id,Definition definition)
    {...}

to

 [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Create(int id,DefinitionFormViewModel definition)
        {...}

If my limited understanding is correct, the problem lies with the parameter passed to the controller. You have create a view that accepts a DefinitionFormViewModel, however when you posting back to the server your current code expects a Definition.

(If this is incorrect or downvoted, please explain as I also am eager to learn)

于 2010-05-27T05:57:18.390 回答