How to display the last inserted ID (primary key) of a table in a html textbox that is in a relationship with a foreign key of another table in mvc

Jon A

I'm new to asp.net and MVC, and I have a problem.
I know that this is something simple but I do not know how to do it. I seek advice and would like to thank you in advance for any help.

This is my problem:
I have 2 tables: table X: ID (primary key), Number; and table Y: ID (primary key), NID (foreign key with relationship with table X), etc.

What I want to know is how to display last inserted ID into the view of table Y on an Html editor for NID the last value of ID (table X)?
For example, I create a new row in table X, and when I want to create the row in table Y that corresponds with table X to automatically get the last ID inserted in the textbox or editor?

Can anybody give me some kind of reference or an example! Thank you for your help! Sorry for any bad spelling.

Yashveer Singh

Here we go . I tested this and it returned me the model properties along with files posted . This example gives you ideea how POSt method used in MVC and how to send model propertied back to controller .

      //-- this is the controller
     public class FileUploadDemoController : Controller
    {
        //
        // GET: /FileUploadDemo/

        public ActionResult Index()
        {
            // here find the last if of the FileUploadtable 
            var ctx = new TestDbContext();
            var maxId = ctx.Fileuploads.ToList().OrderByDescending(u => u.Id).FirstOrDefault();
            var newId = maxId == null ? 1 : maxId.Id + 1;
            return View("Index", new FileUploadModel { Id= newId });
        }


        [HttpPost]
        public ActionResult PostForm(FileUploadModel model)
        {
            // here you have NewId in model.Id method ; Now ypour table b in my case is fileeuploadhistory I want to insert a new record with this model.Id 

            using (var ctx = new TestDbContext())
            {
                var curretFile = ctx.Fileuploads.FirstOrDefault(x => x.Id == model.Id);
                if (curretFile==null)
                {
                    curretFile=new FileUploadModel {   Name=model.Name , ValidFromDate= model.ValidFromDate};                        

                }
                curretFile.History = new FileUploadHistory { InsertedDate = DateTime.Now };
                ctx.Fileuploads.Add(curretFile);
                ctx.SaveChanges();

            }
            return View("Index", model);
        }
    }

-- These are MY EntityFramework entities and I am using same on Views as well

  public class FileUploadModel
    {

        public FileUploadModel()
        {

        }
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Name { get; set; }
        public string ValidFromDate { get; set; }

        public int HistoryId { get; set; }

        [ForeignKeyAttribute("HistoryId")]
        public virtual FileUploadHistory History { get; set; }
    }

    public class FileUploadHistory
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public DateTime InsertedDate { get; set; }
    }

-- Finaly the cshml file . The import point is to use new { enctype = "multipart/form-data" } inside BeginForm . // the page from where you will post the data . Please change you model class in place of FileUploadModel I created for me .

   @model WebApplication1.Models.FileUploadModel



        @using (Html.BeginForm("PostForm", "FileUploadDemo", FormMethod.Post, new { enctype = "multipart/form-data" }))
  {

   <div class="panel">
    <div class="panel-body">

        <div class="form-group row">
            <div class="col-md-2 form-label">
                <label>ID:</label>
            </div>
            <div class="col-md-6">
                @Html.TextAreaFor(x => x.Id , new { @class = "form-control" })
            </div>

        </div>


        <div class="form-group row">
            <div class="col-md-2 form-label">
                <label>Name:</label>
            </div>
            <div class="col-md-6">
                @Html.TextAreaFor(x => x.Name, new { @class = "form-control" })
            </div>

        </div>

        <div class="form-group row">
            <div class="col-md-2 form-label">
                <label>Date</label>
            </div>
            <div class="col-md-6">
                @Html.TextAreaFor(x => x.ValidFromDate, new { @class = "form-control" })
            </div>

        </div>
        <div class="col-md-10">
            <div class="form-group row">
                <div class="col-md-2 form-label">
                    <label>Select File<i class="required-field">*</i>:</label>
                </div>
                <div class="col-md-8">
                    <input type="file" class="file-upload" style="margin: 0px;" hidden="hidden" accept=".xlsx" name="file" id="file" />
                </div>
            </div>
        </div>

        <div class="form-group row">
            <div class="col-md-3 pull-right text-right">
                <button class="btn btn-primary" id="process-submission" type="submit">
                    Submit
                </button>

            </div>
        </div>
    </div>
</div>
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I ensure that a foreign key exists as a primary key in another table when data is inserted?

From Dev

Mapping of primary key as foreign key to another table

From Dev

create a foreign key on a primary key of another table

From Dev

How to get last inserted ID after insert to MySQL table with multi col primary key

From Dev

Update primary key in one table which is foreign key in another table

From Dev

How to add a primary (Surrogate) key from one table into a foreign key of another table?

From Dev

How to store LAST_INSERT_ID() in database as foreign key in another table

From Dev

How to refer primary key as Foreign to various table

From Dev

MySQL - Inserting Primary Key from one table to another (Foreign Key)

From Dev

I cannot send primary key of one table to another as foreign key?

From Dev

Use Auto incremented unique id primary key as foreign key in another table

From Dev

SELECT a non ID column in a foreign key table (TABLE B) based on the foreign key in the primary table (TABLE

From Dev

Finding Primary Table for a Foreign Key

From Dev

How do I get the primary key being referenced by a foreign key of another table?

From Dev

How to save auto generated primary key Id in foreign key column in same table

From Dev

Understanding how a Primary Key of one Table can be a Foreign Key too

From Dev

How to add data if the primary key is used as foreign key in the same table?

From Dev

cakephp: how to display foreign key table field

From Dev

Use of Primary Key as Foreign Key in Foreign Key Table

From Dev

How to create a table with _id as composite primary key?

From Dev

How to create a table with _id as composite primary key?

From Dev

how to get last inserted primary key

From Dev

how to get last inserted primary key

From Dev

How to insert Primary Key value of the primary table to the Foreign Key column of the child table in MySQL?

From Dev

Update Column Of All Rows In A Table From Another Table Using Primary Key And Foreign Key

From Dev

insert a primary key from one table as a foreign key to another table with php lastInsertId()

From Dev

Get foreign key field from primary key in another table that linked to many table

From Dev

how to connect two table by the use of primary-foreign key?

From Dev

how to update foreign key that depends on 3 field in primary table

Related Related

  1. 1

    How do I ensure that a foreign key exists as a primary key in another table when data is inserted?

  2. 2

    Mapping of primary key as foreign key to another table

  3. 3

    create a foreign key on a primary key of another table

  4. 4

    How to get last inserted ID after insert to MySQL table with multi col primary key

  5. 5

    Update primary key in one table which is foreign key in another table

  6. 6

    How to add a primary (Surrogate) key from one table into a foreign key of another table?

  7. 7

    How to store LAST_INSERT_ID() in database as foreign key in another table

  8. 8

    How to refer primary key as Foreign to various table

  9. 9

    MySQL - Inserting Primary Key from one table to another (Foreign Key)

  10. 10

    I cannot send primary key of one table to another as foreign key?

  11. 11

    Use Auto incremented unique id primary key as foreign key in another table

  12. 12

    SELECT a non ID column in a foreign key table (TABLE B) based on the foreign key in the primary table (TABLE

  13. 13

    Finding Primary Table for a Foreign Key

  14. 14

    How do I get the primary key being referenced by a foreign key of another table?

  15. 15

    How to save auto generated primary key Id in foreign key column in same table

  16. 16

    Understanding how a Primary Key of one Table can be a Foreign Key too

  17. 17

    How to add data if the primary key is used as foreign key in the same table?

  18. 18

    cakephp: how to display foreign key table field

  19. 19

    Use of Primary Key as Foreign Key in Foreign Key Table

  20. 20

    How to create a table with _id as composite primary key?

  21. 21

    How to create a table with _id as composite primary key?

  22. 22

    how to get last inserted primary key

  23. 23

    how to get last inserted primary key

  24. 24

    How to insert Primary Key value of the primary table to the Foreign Key column of the child table in MySQL?

  25. 25

    Update Column Of All Rows In A Table From Another Table Using Primary Key And Foreign Key

  26. 26

    insert a primary key from one table as a foreign key to another table with php lastInsertId()

  27. 27

    Get foreign key field from primary key in another table that linked to many table

  28. 28

    how to connect two table by the use of primary-foreign key?

  29. 29

    how to update foreign key that depends on 3 field in primary table

HotTag

Archive