I want to search information of a table with EF

Asked

Viewed 75 times

2

I have the following form code:

@model Domain.Entities.Tabela1
@using WebUI.HtmlHelpers
@using WebUI.Extensions
@{
    ViewBag.Title = "Tabela";
    Layout = "~/Views/Shared/_Master.cshtml";
}
<article ng-app="registrationModule">
    <section>
        <div>
            <div ng-controller="Tabela1FormController">
                <form class="form" name="tabela1Form" ng-submit="save(tabela1)" novalidate style="width: 700px; margin-left: auto; margin-right: auto;">

                    </div>

                    <div>
                        <div>
                            <label for="name">Nome:</label>
                                <input type="text" name="name" ng-model="tabela1.name" />
                        </div>
                    </div> 
//Aqui eu gostaria de busca a informação
<input type="text" name="name" ng-model="tabela2.medida" /> 

My Entities Table 1:

[Table("Tabela1")]
    public class Tabela1
    {
        [Key]
        public int tabela1ID{ get; set; }

        //Strings
        [Required(ErrorMessage = "O campo \"Nome\" é obrigatório")]
        public string Name { get; set; }
        [Required(ErrorMessage="O campo \"Unidade de Medida\" é obrigatório")]
        public string medida{ get; set; }


        //Collections
        public ICollection<Coleçao> campo { get; set; }


        public tabela1()
        {
            campo= new HashSet<Coleçao>();   
}        

Entitie tabela2:

public class tabela2
    {
        public static tabela2 Default = new tabela2();

        [Key]
        public int tabela2ID { get; set; }
        public string Unit { get; set; }
        [Required(ErrorMessage = "Digite um símbolo da unidade de medida")]
        public string Symbol { get; set; }

    } 

Listview Tabela1

 public class tabela1ListViewModel : PaginationViewModel
    {
        public IEnumerable<tabela1> tabela1{ get; set; }
    }

My control:

  public ActionResult Newtabelar()
    {
        return View("tabelaForm");
    }

[Permission(Path = IndicatorPath.tabela, Permission = PermissionType.ReadWrite)]
public ActionResult EditItabela(int tabelaid, bool onlyRead = false)
{
    var tabela = tabela1Repository
        .tabela1
        .Include(x => x.tabela3)
            .Include(x => x.tabela4)
            .Include(x => x.tabela5)
            .Include(x => x.tabela6)
        .FirstOrDefault(x => x.tabela1id == tabela1id);

    ViewBag.OnlyRead = onlyRead;

    return View("tabela1Form", tabela);
}

Well to Tabela1 one he seeks ok, but the table2 want to play the field she has in a select in the form it does not search, my view was made with angular, as I must proceed to solve this problem. thanks in advance.

  • What do you mean "no search", can you explain better? Error? What code are you using to get the table values?

  • I cannot bring the information from table 2 to the desired field.

  • Tabela1 is related to two others and table2 is not.

  • What exactly do you want to access that you’re not getting?

  • i want to access the Unit field of the table2, I do not know if by the angle I take the information and put in the <input type="text" name="name" ng-model="table2.measure" />

1 answer

3


You can use a Viewmodel:

public class ViewModel1
{
   public Tabela1 tabela1 { get; set; }
   public Tabela2 tabela2 { get; set; }
}

Action:

public ActionResult Index()
{
    var viewModel = new ViewModel1();

    // Preencha as propriedades do objeto viewModel1

    return View(viewModel);
}

Then you can use the table2 model in your View.

  • I’ll try there thanks

  • unfortunately unable to implement the code

  • what was the difficulty?

  • Fill the properties of the viewModel1 object

  • The properties are Tabela1 and table2, you must instantiate them, fill the objects as you wish, and then use in your view

Browser other questions tagged

You are not signed in. Login or sign up in order to post.