Domanda

Il mio modello eredita da un'interfaccia:

public interface IGrid
{
    ISearchExpression Search { get; set; }
    .
    .
}

public interface ISearchExpression 
{
    IRelationPredicateBucket Get();
}

Il modello:

public class Project : IGrid
{
      public ISearchExpression Search { get; set; }

      public Project()
      {
            this.Search = new ProjectSearch();
      }  
}

Il ProjectSearch:

public class ProjectSearch: ISearchExpression 
{
    public string Name { get; set; }
    public string Number { get; set; }

    public IRelationPredicateBucket Get()
    {...}
}

E il forte PartialView digitato nella vista principale:

       <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ProjectSearch>" %>

       <%= Html.TextBoxFor(x=>x.Name)%>

       <%= Html.TextBoxFor(x => x.Number)%>

       ....

Quando ho inviato il modulo, la proprietà Search non ottenere legato in modo corretto. Tutto è empty.The azione prende un argomento di tipo ProjectSearch.

Perché il Search non ottenere legato come supposto?

Modifica

L'azione

public virtual ActionResult List(Project gridModel)
{..}
È stato utile?

Soluzione

È necessario specificare il prefisso corretto al fine di impegnare sottotipi. Per esempio, se si desidera associare a Name di proprietà della proprietà Search del Modello vostra casella di testo deve essere denominato: Search.Name. Quando si utilizza Html.TextBoxFor(x=>x.Name) vostra casella di testo è chiamato Name e il modello legante non funziona. Una soluzione è quella di indicare esplicitamente il nome:

<%= Html.TextBox("Search.Name") %>

editore modelli che è una nuova funzionalità di ASP.NET MVC 2.0


UPDATE:

In base alle informazioni aggiuntive fornite nella sezione commenti Ecco un esempio che dovrebbe funzionare:

Modello:

public interface IRelationPredicateBucket
{ }

public interface ISearchExpression
{
    IRelationPredicateBucket Get();
}

public interface IGrid
{
    ISearchExpression Search { get; set; }
}

public class ProjectSearch : ISearchExpression
{
    public string Name { get; set; }
    public string Number { get; set; }

    public IRelationPredicateBucket Get()
    {
        throw new NotImplementedException();
    }
}

public class Project : IGrid
{
    public Project()
    {
        this.Search = new ProjectSearch();
    }

    public ISearchExpression Search { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new Project());
    }

    [HttpPost]
    public ActionResult Index(ProjectSearch gridModel)
    {
        // gridModel.Search should be correctly bound here
        return RedirectToAction("Index");
    }
}

Visualizza - ~ / Vista / Home / Index.aspx:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Models.Project>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using (Html.BeginForm()) { %>
        <% Html.RenderPartial("~/Views/Home/SearchTemplate.ascx", Model.Search); %>
        <input type="submit" value="Create" />
    <% } %>
</asp:Content>

Visualizza - ~ / Vista / Home / SearchTemplate.ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Models.ProjectSearch>" %>

<%= Html.TextBoxFor(x => x.Name) %>
<%= Html.TextBoxFor(x => x.Number) %>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top