question

PeterNewman-6858 avatar image
0 Votes"
PeterNewman-6858 asked LanHuang-MSFT commented

MVC Project

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")





dotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered DuaneArnold-0443 edited

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MJ-8053 avatar image
0 Votes"
MJ-8053 answered

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" />


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterNewman-6858 avatar image
0 Votes"
PeterNewman-6858 answered LanHuang-MSFT commented

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


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @PeterNewman-6858,
I tested your code, new_licenceid_id is not empty, maybe you can use debug to see where there is a problem.
142388-1.png
Best regards,
Lan Huang


0 Votes 0 ·
1.png (63.3 KiB)