开发者

Generic data input form in asp.net mvc application

I have an application that have EF 16 classes that share this information: They all are classes only with a key field and a description. I think it should be a waste if I make a controller with just 1 method just to present a form to fill these classes info, then I was thinking in to make a generic form(with key, description) and dynamically fill the right class through a sort of selection the select开发者_StackOverflow中文版ed info in any way, any good suggestion or pattern to do that? Where the generic methods should be located.


Have you looked into MVC templates? You should be able to use templates to automatically "generate" your Edit and Display Views. No need to create a distinct View for each of your classes.


I had similar situation and did it almost like that:

interface IKeyDescription
{
    int Key { get; set; }
    string Description { get; set; }
}

public partial class Class1 : IKeyDescription;
public partial class Class2 : IKeyDescription;

public abstract class BaseKeyDescriptionController<T> where T : IKeyDescription
{
    [Inject]
    public IKeyDescriptionService<T> Service { get; set; }

    [HttpGet]
    public ActionResult List()
    {
         //View is stored in shared folder
         return View("List",Service.List());
    }

    [HttpPost]
    public ActionResult List(IList<T> elements)
    {
         Service.Save(elements);
         ....
    }
} 

public class Class1Controller : BaseKeyDescriptionController<Class1>
{
}

public class Class2Controller : BaseKeyDescriptionController<Class2>
{
}

View will inherit from System.Web.Mvc.ViewPage<IKeyDescription>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜