sarath
sarath

Reputation: 314

pass data from partial view to other partial view

I have the partial view .cshtml page as

$("#btnsearch").click(function () {
    dserial = $("#dserial").val();
    $('.content').load("/ServiceMaintenance/SearchInstallationDeviceDetails");
})

Here "dserial" is the id of text box, "btnsearch" is the id of the button and "content" is the id[class] of a div to which the partial view is to be loaded.

My need is to assign the value of 'dserial' to a readonly textbox (let the id be "serialno") in the view SearchInstallationDeviceDetails.cshtml which is in the Controller "ServiceMaintenance"

Upvotes: 0

Views: 82

Answers (2)

thelettuce
thelettuce

Reputation: 56

Could you fill it in after the content has loaded like this?

$('.content').load("/ServiceMaintenance/SearchInstallationDeviceDetails",
function() {
  $('.content #serialno').val(dserial);
});

Upvotes: 0

fdomn-m
fdomn-m

Reputation: 28621

1 Change the action parameters, assuming a controller action:

public ActionResult SearchInstallationDeviceDetails()

change to

public ActionResult SearchInstallationDeviceDetails(string dserial)

(or int/guid if string is not appropriate)

then change your js to pass the parameter:

$('.content').load(
              "/ServiceMaintenance/SearchInstallationDeviceDetails",
              { dserial : dserial });

Note: this only works if it's a simple ID, if it's not a simple ID you'll have to use POST

then update your partial view's viewmodel to pass dserial and in the view:

@Html.TextBoxFor(model=>model.dserial, ...

2 Inject the value after the partial has loaded.

Not as nice, but depends on your restrictions (eg if you can't change the controller). You might get a sort of FOUC (flash of unstyled content) - in this case 'un-entered'content

$('.content').load(
          "/ServiceMaintenance/SearchInstallationDeviceDetails",
          function() {
              $("#serialno").val(dserail).prop("readonly", true);
          });

Upvotes: 1

Related Questions