MVC Project

Peter Newman 66 Reputation points
2021-10-19T12:18:35.013+00:00

I am learning MVC with VS2019 and letting VS create the code. I have a Function Create in the controller

    <HttpPost()>
    <ValidateAntiForgeryToken()>
    Function Create(<Bind(Include:="Id,CreatedBy_Id,CreatedBy_LogicalName,CreatedBy_Name,CreatedOn,new_licenceId_Id")> ByVal new_clientbankdata As new_clientbankdata) As ActionResult
        If ModelState.IsValid Then
            db.new_clientbankdata.Add(new_clientbankdata)
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If
        Return View(new_clientbankdata)
    End Function

How can I pass a value for new_licenceId_Id without it being on the Create.vbhtml form. The new_licenceId_id is a session variable Session("LicenceID")

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,249 questions
0 comments No comments
{count} votes

Accepted answer
  1. Duane Arnold 3,211 Reputation points
    2021-10-20T10:42:52.13+00:00

    You should learn about the various models like a viewmodel that is strong typed to a given view.

    https://deviq.com/terms/kinds-of-models

    https://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc

    hidden field in view for AuthorId that must be know when article is persisted to database

    https://github.com/darnold924/PublishingCompany/blob/master/PublishingCompany/Models/ArticleVM.cs

    Look at the logic on how the hiddenfield is used to hold the AuthorId that is needed for the Article that is being saved on a Create() in the ArticleController.

    https://github.com/darnold924/PublishingCompany/blob/master/PublishingCompany/Views/Article/Create.cshtml

    https://github.com/darnold924/PublishingCompany/blob/master/PublishingCompany/Controllers/ArticleController.cs

    https://github.com/darnold924/PublishingCompany/blob/master/PublishingCompany/Models/ArticleDM.cs

    HTH

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. M J 661 Reputation points
    2021-10-19T22:40:39.077+00:00

    You can pass as a hidden form element.

     @Html.HiddenFor(model => model.new_licenceId_Id)
    

    you can also do as such

    <input type="hidden" name="new_licenseId_Id" value="@Session("LicenseID")" />
    

    You could also assign the value in the controller to a ViewBag and pass to the View that way

    //controller code
    
    ViewBag.LicenseID = Session("LicenseID").ToString()
    
    
    //in the View
    <input type="hidden" name="new_licenseId_Id" value="@ViewBag.LicenseID" />
    
    0 comments No comments

  2. Peter Newman 66 Reputation points
    2021-10-20T08:11:16.727+00:00

    If I'm honest, I am struggling. I am still not getting anything through for new_licenceid_id. I have chopped down the Bind statement only for this post. The record is being created but the Licence Is is empty

           ' GET: clientCRbankdata/Create  
            Function Create() As ActionResult  
                ViewBag.licenceID = "123456789"  
                Return View()  
            End Function  
      
            ' POST: clientCRbankdata/Create  
            <HttpPost()>  
            <ValidateAntiForgeryToken()>          
    Function Create(<Bind(Include:="Id,CreatedBy_Id,CreatedBy_LogicalName,CreatedBy_Name,CreatedOn,new_licenceId_Id")> ByVal new_clientbankdata As new_clientbankdata) As ActionResult  
                If ModelState.IsValid Then  
                    db.new_clientbankdata.Add(new_clientbankdata)  
                    db.SaveChanges()  
                    Return RedirectToAction("Index")  
                End If  
                Return View(new_clientbankdata)  
            End Function  
    

    view

    @ModelType Test.new_clientbankdata  
    @Code  
        ViewData("Title") = "Create"  
        Layout = "~/Views/Shared/_Layout.vbhtml"  
    End Code  
      
    <h2>Create</h2>  
      
    @Using (Html.BeginForm())   
        @Html.AntiForgeryToken()  
      
        @<div class="form-horizontal">  
        <h4>new_clientbankdata</h4>  
        <hr />  
        @Html.ValidationSummary(True, "", New With {.class = "text-danger"})  
        <input type="hidden" name="new_licenceId_Id" value="@ViewBag.licenceID"/>  
        
        <div class="form-group">  
            @Html.LabelFor(Function(model) model.new_ledgerref, htmlAttributes:=New With {.class = "control-label col-md-2"})  
            <div class="col-md-10">  
                @Html.EditorFor(Function(model) model.new_ledgerref, New With {.htmlAttributes = New With {.class = "form-control"}})  
                @Html.ValidationMessageFor(Function(model) model.new_ledgerref, "", New With {.class = "text-danger"})  
            </div>  
        </div>  
      
        <div class="form-group">  
            @Html.LabelFor(Function(model) model.new_sortcode, htmlAttributes:=New With {.class = "control-label col-md-2"})  
            <div class="col-md-10">  
                @Html.EditorFor(Function(model) model.new_sortcode, New With {.htmlAttributes = New With {.class = "form-control"}})  
                @Html.ValidationMessageFor(Function(model) model.new_sortcode, "", New With {.class = "text-danger"})  
            </div>  
        </div>  
        
        <div class="form-group">  
            @Html.LabelFor(Function(model) model.new_accountnumber, htmlAttributes:=New With {.class = "control-label col-md-2"})  
            <div class="col-md-10">  
                @Html.EditorFor(Function(model) model.new_accountnumber, New With {.htmlAttributes = New With {.class = "form-control"}})  
                @Html.ValidationMessageFor(Function(model) model.new_accountnumber, "", New With {.class = "text-danger"})  
            </div>  
        </div>  
      
        <div class="form-group">  
            @Html.LabelFor(Function(model) model.new_accountname, htmlAttributes:=New With {.class = "control-label col-md-2"})  
            <div class="col-md-10">  
                @Html.EditorFor(Function(model) model.new_accountname, New With {.htmlAttributes = New With {.class = "form-control"}})  
                @Html.ValidationMessageFor(Function(model) model.new_accountname, "", New With {.class = "text-danger"})  
            </div>  
        </div>  
      
        <div class="form-group">  
            @Html.LabelFor(Function(model) model.new_reference, htmlAttributes:=New With {.class = "control-label col-md-2"})  
            <div class="col-md-10">  
                @Html.EditorFor(Function(model) model.new_reference, New With {.htmlAttributes = New With {.class = "form-control"}})  
                @Html.ValidationMessageFor(Function(model) model.new_reference, "", New With {.class = "text-danger"})  
            </div>  
        </div>  
      
        <div class="form-group">  
            @Html.LabelFor(Function(model) model.new_defaultvalue, htmlAttributes:=New With {.class = "control-label col-md-2"})  
            <div class="col-md-10">  
                @Html.EditorFor(Function(model) model.new_defaultvalue, New With {.htmlAttributes = New With {.class = "form-control"}})  
                @Html.ValidationMessageFor(Function(model) model.new_defaultvalue, "", New With {.class = "text-danger"})  
            </div>  
        </div>  
        <div class="form-group">  
            <div class="col-md-offset-2 col-md-10">  
                <input type="submit" value="Create" class="btn btn-default" />  
            </div>  
        </div>  
    </div>  
    End Using  
      
    <div>  
        @Html.ActionLink("Back to List", "Index")  
    </div>  
      
    @Section Scripts   
        @Scripts.Render("~/bundles/jqueryval")  
    End Section